swiftdata-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SwiftData Code Review

SwiftData代码审查

Quick Reference

快速参考

Issue TypeReference
@Model, @Attribute, @Relationship, delete rulesreferences/model-design.md
@Query, #Predicate, FetchDescriptor, #Indexreferences/queries.md
@ModelActor, ModelContext, background operationsreferences/concurrency.md
VersionedSchema, MigrationStage, lightweight/customreferences/migrations.md
问题类型参考链接
@Model, @Attribute, @Relationship, 删除规则references/model-design.md
@Query, #Predicate, FetchDescriptor, #Indexreferences/queries.md
@ModelActor, ModelContext, 后台操作references/concurrency.md
VersionedSchema, MigrationStage, 轻量/自定义迁移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
  • 模型标记为
    final
    (子类化会导致崩溃)
  • @Relationship装饰器仅在一侧使用(而非两侧)
  • 显式设置删除规则(不依赖默认的.nullify)
  • 关系初始化为空数组,而非默认对象
  • 批量操作用于大量插入(
    append(contentsOf:)
  • @Query不在主线程加载数千条数据
  • 谓词中的外部值捕获到局部变量中
  • 谓词中使用标量比较(而非对象引用)
  • @ModelActor用于后台操作
  • 使用PersistentIdentifier/DTO在Actor之间传递数据
  • 为每个发布版本定义VersionedSchema
  • 向ModelContainer传入MigrationPlan

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
  • 审查@Model或关系时 -> model-design.md
  • 审查@Query或#Predicate时 -> queries.md
  • 审查@ModelActor或后台任务时 -> concurrency.md
  • 审查架构变更或迁移时 -> migrations.md

Review Questions

审查问题

  1. Could this relationship assignment cause NULL foreign keys?
  2. Is @Relationship on both sides creating circular references?
  3. Could this @Query block the main thread with large datasets?
  4. Are model objects being passed between actors unsafely?
  5. Would schema changes require a migration plan?
  1. 该关系赋值是否会导致NULL外键?
  2. 两侧都使用@Relationship是否会造成循环引用?
  3. 该@Query是否会因大数据集阻塞主线程?
  4. 模型对象是否在Actor之间不安全地传递?
  5. 架构变更是否需要迁移计划?