|
| 1 | +--- |
| 2 | +name: swiftdata |
| 3 | +description: Writes, reviews, and improves SwiftData code using modern APIs and best practices. Use when reading, writing, or reviewing projects that use SwiftData. |
| 4 | +--- |
| 5 | + |
| 6 | +Write and review SwiftData code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems - do not nitpick or invent issues. |
| 7 | + |
| 8 | +Review process: |
| 9 | + |
| 10 | +1. Check for core SwiftData issues using `references/core-rules.md`. |
| 11 | +1. Check that predicates are safe and supported using `references/predicates.md`. |
| 12 | +1. If the project uses CloudKit, check for CloudKit-specific constraints using `references/cloudkit.md`. |
| 13 | +1. If the project targets iOS 18+, check for indexing opportunities using `references/indexing.md`. |
| 14 | +1. If the project targets iOS 26+, check for class inheritance patterns using `references/class-inheritance.md`. |
| 15 | + |
| 16 | +If doing partial work, load only the relevant reference files. |
| 17 | + |
| 18 | + |
| 19 | +## Core Instructions |
| 20 | + |
| 21 | +- Target Swift 6.2 or later, using modern Swift concurrency. |
| 22 | +- The user strongly prefers to use SwiftData across the board. Do not suggest Core Data functionality unless it is a feature that cannot be solved with SwiftData. |
| 23 | +- Do not introduce third-party frameworks without asking first. |
| 24 | +- Use a consistent project structure, with folder layout determined by app features. |
| 25 | + |
| 26 | + |
| 27 | +## Output Format |
| 28 | + |
| 29 | +If the user asks for a review, organize findings by file. For each issue: |
| 30 | + |
| 31 | +1. State the file and relevant line(s). |
| 32 | +2. Name the rule being violated. |
| 33 | +3. Show a brief before/after code fix. |
| 34 | + |
| 35 | +Skip files with no issues. End with a prioritized summary of the most impactful changes to make first. |
| 36 | + |
| 37 | +If the user asks you to write or improve code, follow the same rules above but make the changes directly instead of returning a findings report. |
| 38 | + |
| 39 | +Example output: |
| 40 | + |
| 41 | +### Destination.swift |
| 42 | + |
| 43 | +**Line 8: Add an explicit delete rule for relationships.** |
| 44 | + |
| 45 | +```swift |
| 46 | +// Before |
| 47 | +var sights: [Sight] |
| 48 | + |
| 49 | +// After |
| 50 | +@Relationship(deleteRule: .cascade, inverse: \Sight.destination) var sights: [Sight] |
| 51 | +``` |
| 52 | + |
| 53 | +**Line 22: Do not use `isEmpty == false` in predicates – it crashes at runtime. Use `!` instead.** |
| 54 | + |
| 55 | +```swift |
| 56 | +// Before |
| 57 | +#Predicate<Destination> { $0.sights.isEmpty == false } |
| 58 | + |
| 59 | +// After |
| 60 | +#Predicate<Destination> { !$0.sights.isEmpty } |
| 61 | +``` |
| 62 | + |
| 63 | +### DestinationListView.swift |
| 64 | + |
| 65 | +**Line 5: `@Query` must only be used inside SwiftUI views.** |
| 66 | + |
| 67 | +```swift |
| 68 | +// Before |
| 69 | +class DestinationStore { |
| 70 | + @Query var destinations: [Destination] |
| 71 | +} |
| 72 | + |
| 73 | +// After |
| 74 | +class DestinationStore { |
| 75 | + var modelContext: ModelContext |
| 76 | + |
| 77 | + func fetchDestinations() throws -> [Destination] { |
| 78 | + try modelContext.fetch(FetchDescriptor<Destination>()) |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Summary |
| 84 | + |
| 85 | +1. **Data loss (high):** Missing delete rule on line 8 of Destination.swift means sights will be orphaned when a destination is deleted. |
| 86 | +2. **Crash (high):** `isEmpty == false` on line 22 will crash at runtime – use `!isEmpty` instead. |
| 87 | +3. **Incorrect behavior (high):** `@Query` on line 5 of DestinationListView.swift only works inside SwiftUI views. |
| 88 | + |
| 89 | +End of example. |
| 90 | + |
| 91 | + |
| 92 | +## References |
| 93 | + |
| 94 | +- `references/core-rules.md` - autosaving, relationships, delete rules, property restrictions, and FetchDescriptor optimization. |
| 95 | +- `references/predicates.md` - supported predicate operations, dangerous patterns that crash at runtime, and unsupported methods. |
| 96 | +- `references/cloudkit.md` - CloudKit-specific constraints including uniqueness, optionality, and eventual consistency. |
| 97 | +- `references/indexing.md` - database indexing for iOS 18+, including single and compound property indexes. |
| 98 | +- `references/class-inheritance.md` - model subclassing for iOS 26+, including @available requirements, schema setup, and predicate filtering. |
0 commit comments