-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.优化添加资料后列表滚动到头部。 2.增加手册 SwiftData 内容。
- Loading branch information
Showing
9 changed files
with
143 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
SwiftPamphletApp/Resource/Guide/SwiftData/SwiftData-处理大量数据(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,17 @@ | ||
# <#Title#> | ||
|
||
SwiftData 模型上下文有个方法叫 `enumerate()`,可以高效遍历大量数据。 | ||
|
||
```swift | ||
let descriptor = FetchDescriptor<Article>() | ||
... | ||
|
||
do { | ||
try modelContext.enumerate(descriptor, batchSize: 1000) { article in | ||
... | ||
} | ||
} catch { | ||
print("Failed.") | ||
} | ||
``` | ||
|
||
其中 batchSize 参数是调整批量处理的数量,也就是一次加载多少对象。因此可以通过这个值来权衡内存和IO数量。 |
64 changes: 63 additions & 1 deletion
64
SwiftPamphletApp/Resource/Guide/SwiftData/SwiftData-版本迁移(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,64 @@ | ||
# <#Title#> | ||
|
||
以下的小改动 SwiftData 会自动执行轻量迁移: | ||
|
||
- 增加模型 | ||
- 增加有默认值的新属性 | ||
- 重命名属性 | ||
- 删除属性 | ||
- 增加或删除 `.externalStorage` 或 `.allowsCloudEncryption` 属性。 | ||
- 增加所有值都是唯一属性为 `.unique` | ||
- 调整关系的删除规则 | ||
|
||
其他情况需要用到版本迁移,版本迁移步骤如下: | ||
|
||
- 用 VersionedSchema 创建 SwiftData 模型的版本 | ||
- 用 SchemaMigrationPlan 对创建的版本进行排序 | ||
- 为每个迁移定义一个迁移阶段 | ||
|
||
设置版本 | ||
|
||
```swift | ||
enum ArticleV1Schema: VersionedSchema { | ||
static var versionIdentifier: String? = "v1" | ||
static var models: [any PersistentModel.Type] { [Article.self] } | ||
|
||
@Model | ||
final class Article { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
SchemaMigrationPlan 轻量迁移 | ||
|
||
```swift | ||
enum ArticleMigrationPlan: SchemaMigrationPlan { | ||
static var schemas: [any VersionedSchema.Type] { | ||
[ArticleV1Schema.self, ArticleV2Schema.self] | ||
} | ||
|
||
static var stages: [MigrationStage] { | ||
[migrateV1toV2] | ||
} | ||
|
||
static let migrateV1toV2 = MigrationStage.lightweight( | ||
fromVersion: ArticleV1Schema.self, | ||
toVersion: ArticleV2Schema.self | ||
) | ||
} | ||
``` | ||
|
||
自定义迁移 | ||
|
||
```swift | ||
static let migrateV1toV2 = MigrationStage.custom( | ||
fromVersion: ArticleV1Schema.self, | ||
toVersion: ArticleV2Schema.self, | ||
willMigrate: { context in | ||
// 合并前的处理 | ||
}, | ||
didMigrate: { context in | ||
// 合并后的处理 | ||
} | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# <#Title#> | ||
|
||
CoreData 的调试方式依然适用于 SwiftData。 | ||
|
||
你可以设置启动参数来让 CoreData 打印出执行的 SQL 语句。在你的项目中,选择 "Product" -> "Scheme" -> "Edit Scheme",然后在 "Arguments" 标签下的 "Arguments Passed On Launch" 中添加 -com.apple.CoreData.SQLDebug 1。这样,每当 CoreData 执行 SQL 语句时,都会在控制台中打印出来。 | ||
|
||
使用 `-com.apple.CoreData.SQLDebug 3` 获取后台更多信息。 |
9 changes: 9 additions & 0 deletions
9
SwiftPamphletApp/Resource/Guide/SwiftData/SwiftData-资料(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
## WWDC | ||
|
||
23 | ||
- [Dive deeper into SwiftData - WWDC23 - Videos - Apple Developer](https://developer.apple.com/wwdc23/10196) | ||
- [Migrate to SwiftData - WWDC23 - Videos - Apple Developer](https://developer.apple.com/wwdc23/10189) | ||
- [Meet SwiftData - WWDC23 - Videos - Apple Developer](https://developer.apple.com/wwdc23/10187) | ||
- [Model your schema with SwiftData - WWDC23 - Videos - Apple Developer](https://developer.apple.com/wwdc23/10195) | ||
- [Build an app with SwiftData - WWDC23 - Videos - Apple Developer](https://developer.apple.com/wwdc23/10154) |
27 changes: 26 additions & 1 deletion
27
SwiftPamphletApp/Resource/Guide/SwiftData/SwiftData多线程(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
# <#Title#> | ||
|
||
创建一个 Actor,然后 SwiftData 上下文在其中执行操作。 | ||
|
||
```swift | ||
@ModelActor | ||
actor DataHandler {} | ||
|
||
extension DataHandler { | ||
func addInfo() throws -> IOInfo { | ||
let info = IOInfo() | ||
modelContext.insert(info) | ||
try modelContext.save() | ||
return info | ||
} | ||
... | ||
} | ||
``` | ||
|
||
使用 | ||
|
||
```swift | ||
Task.detached { | ||
let handler = DataHandler() | ||
let item = try await handler.addInfo() | ||
... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.