Skip to content

Commit

Permalink
更新结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed May 7, 2024
1 parent 4408529 commit 62c937c
Show file tree
Hide file tree
Showing 35 changed files with 171 additions and 53 deletions.
84 changes: 52 additions & 32 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@
08659BCD2BE9A40A009B7C00 /* 创建@Model模型(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BCC2BE9A40A009B7C00 /* 创建@Model模型(ap).md */; };
08659BCF2BE9A430009B7C00 /* 容器配置modelContainer(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BCE2BE9A430009B7C00 /* 容器配置modelContainer(ap).md */; };
08659BD12BE9A448009B7C00 /* 增删modelContext(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD02BE9A448009B7C00 /* 增删modelContext(ap).md */; };
08659BD32BE9A478009B7C00 /* 检索@Query(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD22BE9A478009B7C00 /* 检索@Query(ap).md */; };
08659BD52BE9A499009B7C00 /* 检索FetchDescriptor(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD42BE9A499009B7C00 /* 检索FetchDescriptor(ap).md */; };
08659BD32BE9A478009B7C00 /* SwiftData-检索(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD22BE9A478009B7C00 /* SwiftData-检索(ap).md */; };
08659BD72BE9A7F8009B7C00 /* SwiftData-处理大量数据(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD62BE9A7F8009B7C00 /* SwiftData-处理大量数据(ap).md */; };
08659BD92BE9A80E009B7C00 /* SwiftData多线程(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BD82BE9A80E009B7C00 /* SwiftData多线程(ap).md */; };
08659BDB2BE9A834009B7C00 /* SwiftData-版本迁移(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08659BDA2BE9A834009B7C00 /* SwiftData-版本迁移(ap).md */; };
Expand Down Expand Up @@ -369,8 +368,7 @@
08659BCC2BE9A40A009B7C00 /* 创建@Model模型(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "创建@Model模型(ap).md"; sourceTree = "<group>"; };
08659BCE2BE9A430009B7C00 /* 容器配置modelContainer(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "容器配置modelContainer(ap).md"; sourceTree = "<group>"; };
08659BD02BE9A448009B7C00 /* 增删modelContext(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "增删modelContext(ap).md"; sourceTree = "<group>"; };
08659BD22BE9A478009B7C00 /* 检索@Query(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "检索@Query(ap).md"; sourceTree = "<group>"; };
08659BD42BE9A499009B7C00 /* 检索FetchDescriptor(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "检索FetchDescriptor(ap).md"; sourceTree = "<group>"; };
08659BD22BE9A478009B7C00 /* SwiftData-检索(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "SwiftData-检索(ap).md"; sourceTree = "<group>"; };
08659BD62BE9A7F8009B7C00 /* SwiftData-处理大量数据(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "SwiftData-处理大量数据(ap).md"; sourceTree = "<group>"; };
08659BD82BE9A80E009B7C00 /* SwiftData多线程(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "SwiftData多线程(ap).md"; sourceTree = "<group>"; };
08659BDA2BE9A834009B7C00 /* SwiftData-版本迁移(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "SwiftData-版本迁移(ap).md"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -512,7 +510,10 @@
isa = PBXGroup;
children = (
08449027279ECEA200B61353 /* 介绍 */,
0844902A279ECED300B61353 /* 视图组件使用 */,
08A7FF2D2BEABDCA00E12E5A /* 图文组件 */,
08A7FF2E2BEABE0100E12E5A /* 数据集合组件 */,
08A7FF2F2BEABE3F00E12E5A /* 布局组件 */,
0844902A279ECED300B61353 /* 表单组件 */,
08522BE727CF625B005FF059 /* 视觉 */,
08659BC62BE8FD84009B7C00 /* SwiftUI数据流(ap).md */,
);
Expand Down Expand Up @@ -767,45 +768,27 @@
children = (
08449028279ECEB100B61353 /* SwiftUI是什么(ap).md */,
08026C3828699EB700792EF1 /* SwiftUI参考资料(ap).md */,
0844902B279ECEFB00B61353 /* SwiftUI对标的UIKit视图(ap).md */,
);
path = "介绍";
sourceTree = "<group>";
};
0844902A279ECED300B61353 /* 视图组件使用 */ = {
0844902A279ECED300B61353 /* 表单组件 */ = {
isa = PBXGroup;
children = (
0844902B279ECEFB00B61353 /* SwiftUI对标的UIKit视图(ap).md */,
0844902D279ECF1C00B61353 /* Text(ap).md */,
08BE632B27BE3762002BC6A8 /* Link(ap).md */,
08BE632F27BE6CAA002BC6A8 /* Label(ap).md */,
08BE634127BFAF76002BC6A8 /* TextEditor(ap).md */,
08BE634327C3845E002BC6A8 /* TextField(ap).md */,
08026C3D2869A51200792EF1 /* Form(ap).md */,
0896FB9127BA486900676B7F /* Button(ap).md */,
08BE636727C8C2A0002BC6A8 /* 进度(ap).md */,
08BE636B27C8CFA7002BC6A8 /* Image(ap).md */,
08BE635727C63F3A002BC6A8 /* ControlGroup(ap).md */,
08BE635B27C65C7C002BC6A8 /* GroupBox(ap).md */,
08BE634927C4BDDB002BC6A8 /* Stack(ap).md */,
08026C3A2869A21000792EF1 /* Advanced layout control(ap).md */,
08BE635F27C7A673002BC6A8 /* Navigation(ap).md */,
08BE635327C63828002BC6A8 /* List(ap).md */,
08BE636327C886D2002BC6A8 /* LazyVStack和LazyHStack(ap).md */,
08BE636F27C8F6A7002BC6A8 /* LazyVGrid和LazyHGrid(ap).md */,
08026C502869B41B00792EF1 /* table(ap).md */,
08BE637327CCAB52002BC6A8 /* ScrollView(ap).md */,
08BE638E27CE157D002BC6A8 /* 浮层(ap).md */,
08C3BB7F27CE4A8500ACF0FE /* TabView(ap).md */,
08026C392869A17800792EF1 /* SwiftCharts(ap).md */,
08BE636727C8C2A0002BC6A8 /* 进度(ap).md */,
08C3BBA127CF1B2B00ACF0FE /* Toggle(ap).md */,
08522BD527CF3218005FF059 /* Picker(ap).md */,
08522BD927CF5029005FF059 /* Slider(ap).md */,
08522BDD27CF5133005FF059 /* Stepper(ap).md */,
08026C3D2869A51200792EF1 /* Form(ap).md */,
08522BEC27CF7A0C005FF059 /* Keyboard(ap).md */,
08026C3B2869A2F100792EF1 /* Transferable(ap).md */,
08026C3C2869A35800792EF1 /* ShareLink(ap).md */,
);
path = "视图组件使用";
path = "表单组件";
sourceTree = "<group>";
};
0846234D2BAC59AF003373D9 /* App */ = {
Expand Down Expand Up @@ -846,6 +829,7 @@
085BB77327D22FCA00E8F69A /* SwiftUI动画(ap).md */,
085BB77527D22FE300E8F69A /* SwiftUI Canvas(ap).md */,
08026C3E2869AD7800792EF1 /* SF Symbol(ap).md */,
08026C392869A17800792EF1 /* SwiftCharts(ap).md */,
);
path = "视觉";
sourceTree = "<group>";
Expand All @@ -857,8 +841,7 @@
08659BDE2BEA4D8C009B7C00 /* SwiftData-模型关系(ap).md */,
08659BCE2BE9A430009B7C00 /* 容器配置modelContainer(ap).md */,
08659BD02BE9A448009B7C00 /* 增删modelContext(ap).md */,
08659BD22BE9A478009B7C00 /* 检索@Query(ap).md */,
08659BD42BE9A499009B7C00 /* 检索FetchDescriptor(ap).md */,
08659BD22BE9A478009B7C00 /* SwiftData-检索(ap).md */,
08659BD62BE9A7F8009B7C00 /* SwiftData-处理大量数据(ap).md */,
08659BD82BE9A80E009B7C00 /* SwiftData多线程(ap).md */,
08659BDA2BE9A834009B7C00 /* SwiftData-版本迁移(ap).md */,
Expand Down Expand Up @@ -960,6 +943,44 @@
path = Guide;
sourceTree = "<group>";
};
08A7FF2D2BEABDCA00E12E5A /* 图文组件 */ = {
isa = PBXGroup;
children = (
0844902D279ECF1C00B61353 /* Text(ap).md */,
08BE632B27BE3762002BC6A8 /* Link(ap).md */,
08BE632F27BE6CAA002BC6A8 /* Label(ap).md */,
08BE634127BFAF76002BC6A8 /* TextEditor(ap).md */,
08BE634327C3845E002BC6A8 /* TextField(ap).md */,
08BE636B27C8CFA7002BC6A8 /* Image(ap).md */,
);
path = "图文组件";
sourceTree = "<group>";
};
08A7FF2E2BEABE0100E12E5A /* 数据集合组件 */ = {
isa = PBXGroup;
children = (
08BE637327CCAB52002BC6A8 /* ScrollView(ap).md */,
08BE635327C63828002BC6A8 /* List(ap).md */,
08BE636327C886D2002BC6A8 /* LazyVStack和LazyHStack(ap).md */,
08BE636F27C8F6A7002BC6A8 /* LazyVGrid和LazyHGrid(ap).md */,
08026C502869B41B00792EF1 /* table(ap).md */,
);
path = "数据集合组件";
sourceTree = "<group>";
};
08A7FF2F2BEABE3F00E12E5A /* 布局组件 */ = {
isa = PBXGroup;
children = (
08BE635F27C7A673002BC6A8 /* Navigation(ap).md */,
08C3BB7F27CE4A8500ACF0FE /* TabView(ap).md */,
08BE634927C4BDDB002BC6A8 /* Stack(ap).md */,
08BE635727C63F3A002BC6A8 /* ControlGroup(ap).md */,
08BE635B27C65C7C002BC6A8 /* GroupBox(ap).md */,
08026C3A2869A21000792EF1 /* Advanced layout control(ap).md */,
);
path = "布局组件";
sourceTree = "<group>";
};
08AEAEE7277EDD5D00B969E2 /* Guide */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1215,7 +1236,6 @@
08449004279ECB2900B61353 /* prepend(ap).md in Resources */,
08BE634A27C4BDDB002BC6A8 /* Stack(ap).md in Resources */,
08448F89279EB7FD00B61353 /* 网络状态检查(ap).md in Resources */,
08659BD52BE9A499009B7C00 /* 检索FetchDescriptor(ap).md in Resources */,
08449018279ECD2400B61353 /* Swift Concurrency是什么(ap).md in Resources */,
08448FAD279EC2E900B61353 /* 泛型和协议(ap).md in Resources */,
08026C532869B44400792EF1 /* macOS技术演进(ap).md in Resources */,
Expand Down Expand Up @@ -1257,7 +1277,7 @@
0850445827B1228E0096D556 /* Result(ap).md in Resources */,
08449008279ECB6500B61353 /* zip(ap).md in Resources */,
08448F83279EB78A00B61353 /* 版本兼容(ap).md in Resources */,
08659BD32BE9A478009B7C00 /* 检索@Query(ap).md in Resources */,
08659BD32BE9A478009B7C00 /* SwiftData-检索(ap).md in Resources */,
08448F81279EB75800B61353 /* 系统判断(ap).md in Resources */,
08448FF6279EC9E800B61353 /* Just(ap).md in Resources */,
08026C452869B25700792EF1 /* Distributed Actors(ap).md in Resources */,
Expand Down
39 changes: 22 additions & 17 deletions SwiftPamphletApp/Guide/GuideListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,41 @@ final class GuideListModel {
L(t: "SwiftUI",sub: [
L(t: "介绍",sub: [
L(t: "SwiftUI是什么"),
L(t: "SwiftUI参考资料")
]),
L(t: "视图组件使用",sub: [
L(t: "SwiftUI参考资料"),
L(t: "SwiftUI对标的UIKit视图"),
]),
L(t: "图文组件",sub: [
L(t: "Text"),
L(t: "Link"),
L(t: "Label"),
L(t: "TextEditor"),
L(t: "TextField"),
L(t: "Button"),
L(t: "进度"),
L(t: "Image"),
L(t: "ControlGroup"),
L(t: "GroupBox"),
L(t: "Stack"),
L(t: "Advanced layout control"),
L(t: "Navigation"),
]),
L(t: "数据集合组件", sub: [
L(t: "ScrollView"),
L(t: "List"),
L(t: "LazyVStack和LazyHStack"),
L(t: "LazyVGrid和LazyHGrid"),
L(t: "table"),
L(t: "ScrollView"),
L(t: "浮层"),
]),
L(t: "布局组件", sub: [
L(t: "Navigation"),
L(t: "TabView"),
L(t: "SwiftCharts"),
L(t: "Stack"),
L(t: "ControlGroup"),
L(t: "GroupBox"),
L(t: "Advanced layout control"),
]),
L(t: "视图组件使用",sub: [
L(t: "Form"),
L(t: "Button"),
L(t: "进度"),
L(t: "浮层"),
L(t: "Toggle"),
L(t: "Picker"),
L(t: "Slider"),
L(t: "Stepper"),
L(t: "Form"),
L(t: "Keyboard"),
L(t: "Transferable"),
L(t: "ShareLink")
Expand All @@ -192,7 +197,8 @@ final class GuideListModel {
L(t: "SwiftUI Effect"),
L(t: "SwiftUI动画"),
L(t: "SwiftUI Canvas"),
L(t: "SF Symbol")
L(t: "SF Symbol"),
L(t: "SwiftCharts"),
]),
L(t: "SwiftUI数据流")
]),
Expand Down Expand Up @@ -228,8 +234,7 @@ final class GuideListModel {
L(t: "SwiftData-模型关系"),
L(t: "容器配置modelContainer"),
L(t: "增删modelContext"),
L(t: "检索@Query"),
// L(t: "检索FetchDescriptor"),
L(t: "SwiftData-检索"),
// L(t: "SwiftData-处理大量数据"),
// L(t: "SwiftData多线程"),
// L(t: "SwiftData-版本迁移"),
Expand Down
97 changes: 97 additions & 0 deletions SwiftPamphletApp/Resource/Guide/SwiftData/SwiftData-检索(ap).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

## `@Query`
使用 `@Query` 会从数据库中获取数据。

```swift
@Query private var articles: [Article]
```

`@Query` 还支持 filter、sort、order 和 animation 等参数。

```swift
@Query(sort: \Article.title, order: .forward) private var articles: [Article]
```

sort 可支持多个 SortDescriptor,SwiftData 会按顺序处理。

```swift
@Query(sort: [SortDescriptor(\Article.isArchived, order: .forward),SortDescriptor(\Article.updateDate, order: .reverse)]) var articles: [Article]
```

## Predicate

filter 使用的是 `#Predicate`

```swift
static var now: Date { Date.now }

@Query(filter: #Predicate<Article> { article in
article.releaseDate > now
}) var draftArticles: [Article]
```

Predicate 支持的内置方法主要有 `contains``allSatisfy``flatMap``filter``subscript``starts``min``max``localizedStandardContains``localizedCompare``caseInsensitiveCompare` 等。

```swift
@Query(filter: #Predicate<Article> { article in
article.title.starts(with: "苹果发布会")
}) var articles: [Article]
```

需要注意的是 `.isEmpty` 不能使用 `article.title.isEmpty == false` ,否则会崩溃。

## FetchDescriptor

FetchDescriptor 可以在模型中查找数据,而不必在视图层做。

```swift
@Model
final class Article {
var title: String
...
static var all: FetchDescriptor<Article> {
FetchDescriptor(sortBy: [SortDescriptor(\Article.updateDate, order: .reverse)])
}
}

struct SomeView: View {
@Query(Article.all) private var articles: [Article]
...
}
```

## 获取数量而不加载

使用 `fetchCount()` 方法,可完成整个计数,且很快,内存占用少。

```swift
let descriptor = FetchDescriptor<Article>(predicate: #Predicate { $0.words > 50 })
let count = (try? modelContext.fetchCount(descriptor)) ?? 0
```

## fetchLimit 限制获取数量

```swift
var descriptor = FetchDescriptor<Article>(
predicate: #Predicate { $0.read },
sortBy: [SortDescriptor(\Article.updateDate,
order: .reverse)])
descriptor.fetchLimit = 30
let articles = try context.fetch(descriptor)

// 翻页
let pSize = 30
let pNumber = 1
var fetchDescriptor = FetchDescriptor<Article>(sortBy: [SortDescriptor(\Article.updateDate, order: .reverse)])
fetchDescriptor.fetchOffset = pNumber * pSize
fetchDescriptor.fetchLimit = pSize
```

## 限制获取的属性

只请求要用的属性

```swift
var fetchDescriptor = FetchDescriptor<Article>(sortBy: [SortDescriptor(\.updateDate, order: .reverse)])
fetchDescriptor.propertiesToFetch = [\.title, \.updateDate]
```
2 changes: 0 additions & 2 deletions SwiftPamphletApp/Resource/Guide/SwiftData/检索@Query(ap).md

This file was deleted.

This file was deleted.

0 comments on commit 62c937c

Please sign in to comment.