existential-birds/beagle / plugins/beagle-ios/skills/swiftdata-code-review
swiftdata-code-review
VerifiedReviews SwiftData code for model design, queries, concurrency, and migrations. Use when reviewing .swift files with import SwiftData, @Model, @Query, @ModelActor, or VersionedSchema.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.0 KB |
| references/concurrency.md | markdown | 4.3 KB |
| references/migrations.md | markdown | 4.8 KB |
| references/model-design.md | markdown | 3.3 KB |
| references/queries.md | markdown | 3.8 KB |
references/model-design.md
View on GitHub# SwiftData Model Design
## Best Practices
| Practice | Description |
|----------|-------------|
| Mark models `final` | Subclassing causes runtime crashes |
| Explicit initializers | Required even with default values |
| `var` for relationships | `let` causes runtime crashes |
| Avoid `description` property | Reserved word; use `details` or `content` |
| @Relationship on ONE side | Both sides causes circular reference errors |
| Explicit delete rules | Don't rely on default `.nullify` |
| Optional relationships | Non-optional with `.nullify` crashes |
| Empty array init | `= []` not default objects |
| Batch operations | `append(contentsOf:)` not individual `append()` |
## @Attribute Options
| Option | When to Use |
|--------|-------------|
| `.unique` | Natural identifiers (NOT with CloudKit) |
| `.externalStorage` | Large binary data (images, files) |
| `.spotlight` | User-searchable text content |
| `.transformable` | Custom types needing serialization |
| `.allowsCloudEncryption` | Sensitive synced data |
| `.transient` | Computed/cached values |
## Delete Rules
```swift
@Relationship(deleteRule: .cascade) // Deletes children (owned relationships)
@Relationship(deleteRule: .nullify) // Sets to nil (default, fragile)
@Relationship(deleteRule: .deny) // Prevents deletion if children exist
@Relationship(deleteRule: .noAction) // Does nothing (dangerous!)
```
## Critical Anti-Patterns
### Decorating Both Sides of Relationship
```swift
// BAD: Circular reference error
@Model class Student {
@Relationship(inverse: \TestResult.student) var testResults: [TestResult]
}
@Model class TestResult {
@Relationship(inverse: \Student.testResults) var student: Student?
}
// GOOD: @Relationship on one side only
@Model class Student {
@Relationship(deleteRule: .cascade, inverse: \TestResult.student)
var testResults: [TestResult] = []
}
@Model class TestResult {
var student: Student? // No decorator
}
```
### Assignment in Initializer
```swift
// BAD: Foreign keys become NULL
init(floors: [Floor]) {
self.floors = floors // Bypasses tracking!
}
// GOOD: Use append
init(floors: [Floor]) {
self.floors.append(contentsOf: floors)
}
```
### Default Values for Relationships
```swift
// BAD: Runtime crash
var tag: Tag = Tag(name: "default")
// GOOD: Optional or set after insertion
var tag: Tag?
```
### Individual Appends in Loop
```swift
// BAD: 700x slower than Core Data
for i in 0..<1000 {
item.tags.append(Tag(name: "\(i)"))
}
// GOOD: Batch operation
var tags = (0..<1000).map { Tag(name: "\($0)") }
item.tags.append(contentsOf: tags)
```
### Arrays for Searchable Data
```swift
// BAD: Stored as blob, not searchable
var tags: [String]
// GOOD: Relationship model
@Relationship(deleteRule: .cascade) var tags: [Tag] = []
```
### Unique with CloudKit
```swift
// BAD: Breaks CloudKit sync
@Attribute(.unique) var email: String
// GOOD: Handle uniqueness programmatically
var email: String
```
## Review Questions
- [ ] Is the model marked `final`?
- [ ] Does it have an explicit initializer?
- [ ] Are relationships `var`, not `let`?
- [ ] Is @Relationship on only ONE side of bidirectional relationships?
- [ ] Does delete rule match optionality?
- [ ] Are relationships initialized to `= []`?
- [ ] Are batch operations used for bulk additions?
- [ ] If using CloudKit, are there any `.unique` attributes?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |