SwiftData Code Review
Quick Reference
| Issue Type | Reference |
|---|
| @Model, @Attribute, @Relationship, delete rules | references/model-design.md |
| @Query, #Predicate, FetchDescriptor, #Index |
references/queries.md |
| @ModelActor, ModelContext, background operations |
references/concurrency.md |
| VersionedSchema, MigrationStage, lightweight/custom |
references/migrations.md |
Review Checklist
- - [ ] Models marked
final (subclassing crashes) - [ ] @Relationship decorator on ONE side only (not both)
- [ ] Delete rules explicitly set (not relying on default .nullify)
- [ ] Relationships initialized to empty arrays, not default objects
- [ ] Batch operations used for bulk inserts (
append(contentsOf:)) - [ ] @Query not loading thousands of items on main thread
- [ ] External values in predicates captured in local variables
- [ ] Scalar comparisons in predicates (not object references)
- [ ] @ModelActor used for background operations
- [ ] PersistentIdentifier/DTOs used to pass data between actors
- [ ] VersionedSchema defined for each shipped version
- [ ] MigrationPlan passed to ModelContainer
When to Load References
- - Reviewing @Model or relationships -> model-design.md
- Reviewing @Query or #Predicate -> queries.md
- Reviewing @ModelActor or background work -> concurrency.md
- Reviewing schema changes or migrations -> migrations.md
Review Questions
- 1. Could this relationship assignment cause NULL foreign keys?
- Is @Relationship on both sides creating circular references?
- Could this @Query block the main thread with large datasets?
- Are model objects being passed between actors unsafely?
- Would schema changes require a migration plan?
SwiftData 代码审查
快速参考
references/queries.md |
| @ModelActor、ModelContext、后台操作 |
references/concurrency.md |
| VersionedSchema、MigrationStage、轻量级/自定义迁移 |
references/migrations.md |
审查清单
- - [ ] 模型标记为 final(子类化会导致崩溃)
- [ ] @Relationship 装饰器仅在一侧使用(而非两侧)
- [ ] 明确设置删除规则(不依赖默认的 .nullify)
- [ ] 关系初始化为空数组,而非默认对象
- [ ] 批量插入使用批量操作(append(contentsOf:))
- [ ] @Query 不在主线程加载数千条数据
- [ ] 谓词中的外部值捕获到局部变量中
- [ ] 谓词中使用标量比较(而非对象引用)
- [ ] 后台操作使用 @ModelActor
- [ ] 使用 PersistentIdentifier/DTO 在 actor 之间传递数据
- [ ] 为每个已发布版本定义 VersionedSchema
- [ ] 将 MigrationPlan 传递给 ModelContainer
何时加载参考文档
- - 审查 @Model 或关系时 -> model-design.md
- 审查 @Query 或 #Predicate 时 -> queries.md
- 审查 @ModelActor 或后台工作时 -> concurrency.md
- 审查模式变更或迁移时 -> migrations.md
审查问题
- 1. 这种关系赋值是否可能导致 NULL 外键?
- 两侧都使用 @Relationship 是否会造成循环引用?
- 这个 @Query 是否可能因大数据集阻塞主线程?
- 模型对象是否在不安全的情况下在 actor 之间传递?
- 模式变更是否需要迁移计划?