Skip to content

existential-birds/beagle / plugins/beagle-ios/skills/swiftdata-code-review

swiftdata-code-review

Verified

Reviews SwiftData code for model design, queries, concurrency, and migrations. Use when reviewing .swift files with import SwiftData, @Model, @Query, @ModelActor, or VersionedSchema.

FileKindSize
SKILL.mdskill md3.0 KB
references/concurrency.mdmarkdown4.3 KB
references/migrations.mdmarkdown4.8 KB
references/model-design.mdmarkdown3.3 KB
references/queries.mdmarkdown3.8 KB

references/queries.md

View on GitHub
# SwiftData Queries

## Best Practices

| Practice | Description |
|----------|-------------|
| Local variables for external values | Copy `Date.now` before using in predicate |
| Compare scalars, not objects | Use `id` (UUID), not object references |
| Restrictive checks first | Most selective conditions first |
| `localizedStandardContains()` | Case-insensitive text search |
| `starts(with:)` | `hasPrefix()` is NOT supported |
| `fetchCount()` for counts | Never fetch array just for `.count` |
| `fetchLimit`/`fetchOffset` | Pagination for large datasets |
| #Index for filtered properties | iOS 18+ performance optimization |

## @Query Patterns

```swift
// Static query
@Query var items: [Item]

// Sorted query
@Query(sort: \Item.timestamp, order: .reverse) var items: [Item]

// Multi-field sort
@Query(sort: [SortDescriptor(\Item.priority, order: .reverse),
              SortDescriptor(\Item.name)]) var items: [Item]

// Dynamic filtering in init
init(showCompleted: Bool) {
    let completed = showCompleted  // Capture external value
    _items = Query(filter: #Predicate<Item> { item in
        completed || !item.isCompleted
    })
}
```

## FetchDescriptor Patterns

```swift
var descriptor = FetchDescriptor<Item>(
    predicate: #Predicate { $0.createdAt > cutoffDate },
    sortBy: [SortDescriptor(\.createdAt, order: .reverse)]
)
descriptor.fetchLimit = 50
descriptor.fetchOffset = page * 50
descriptor.propertiesToFetch = [\.id, \.title]

let count = try context.fetchCount(descriptor)  // Count without fetching
let items = try context.fetch(descriptor)
```

## #Index (iOS 18+)

```swift
@Model final class Order {
    #Index<Order>(
        [\.status],              // Filter by status
        [\.createdAt],           // Sort by date
        [\.status, \.createdAt]  // Compound index
    )
    var status: String
    var createdAt: Date
}
```

## Critical Anti-Patterns

### External Values Directly in @Query

```swift
// BAD: Date.now evaluated at macro expansion
@Query(filter: #Predicate<Event> { $0.date > Date.now })
var events: [Event]

// GOOD: Capture in local variable
static var now: Date { Date.now }
@Query(filter: #Predicate<Event> { $0.date > now })
var events: [Event]
```

### Object Comparison in Predicate

```swift
// BAD: Runtime crash
let predicate = #Predicate<Post> { $0.author == selectedUser }

// GOOD: Compare identifier
let userId = selectedUser.id
let predicate = #Predicate<Post> { $0.author.id == userId }
```

### Unsupported String Methods

```swift
// BAD: These crash
item.name.hasPrefix("A")     // Use starts(with:)
item.name.hasSuffix("z")     // Not supported
item.name.uppercased()       // Not translatable

// GOOD: Supported methods
item.name.starts(with: "A")
item.name.localizedStandardContains("test")
```

### Wrong Boolean Comparison

```swift
// BAD: Runtime crash
#Predicate<Movie> { $0.cast.isEmpty == false }

// GOOD: Negation operator
#Predicate<Movie> { !$0.cast.isEmpty }
```

### Fetching Just to Count

```swift
// BAD: Loads all objects into memory
let count = try context.fetch(FetchDescriptor<Item>()).count

// GOOD: Dedicated count method
let count = try context.fetchCount(FetchDescriptor<Item>())
```

### Heavy @Query on Main Thread

```swift
// BAD: Freezes UI
@Query var allPhotos: [Photo]  // 10,000+ items

// GOOD: Pagination in view model
var descriptor = FetchDescriptor<Photo>()
descriptor.fetchLimit = 50
descriptor.fetchOffset = offset
```

## Review Questions

- [ ] Is @Query loading only what the view needs?
- [ ] Are external values captured in local variables?
- [ ] Are comparisons using scalars, not object references?
- [ ] Are string methods limited to supported ones?
- [ ] Is `!isEmpty` used instead of `isEmpty == false`?
- [ ] Is `fetchCount()` used instead of fetching for counts?
- [ ] Are `fetchLimit`/`fetchOffset` used for pagination?
- [ ] Are frequently filtered properties indexed (iOS 18+)?
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: 322kUpdated Claude CodeCodexVerifiedInstall
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: 197kUpdated Claude CodeCodexVerifiedInstall
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: 197kUpdated Claude CodeCodexVerifiedInstall
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: 197kUpdated Claude CodeCodexVerifiedInstall