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/concurrency.md

View on GitHub
# SwiftData Concurrency

## Best Practices

| Practice | Description |
|----------|-------------|
| @ModelActor for background work | Proper thread isolation for SwiftData |
| PersistentIdentifier for cross-actor | Model objects are NOT Sendable |
| Sendable DTOs for data exchange | Create separate types for transfer |
| Task.detached for background | Ensures actor runs off main thread |
| Explicit `save()` in Task | Autosave may not execute in time |
| @Query for display, @ModelActor for mutations | Separate concerns |

## @ModelActor Pattern

```swift
@ModelActor
actor DataHandler {
    func importItems(_ data: [ImportData]) throws {
        for item in data {
            modelContext.insert(Item(name: item.name))
        }
        try modelContext.save()
    }

    func updateItem(id: PersistentIdentifier, name: String) throws {
        guard let item = self[id, as: Item.self] else { return }
        item.name = name
        try modelContext.save()
    }
}
```

## Sendable DTO Pattern

```swift
struct ItemDTO: Sendable, Identifiable {
    let id: PersistentIdentifier
    let name: String
    let timestamp: Date
}

@ModelActor
actor DataService {
    func fetchItems() throws -> [ItemDTO] {
        try modelContext.fetch(FetchDescriptor<Item>())
            .map { ItemDTO(id: $0.persistentModelID, name: $0.name, timestamp: $0.timestamp) }
    }
}
```

## Background Actor Creation

```swift
// GOOD: Ensures background execution
Task.detached {
    let handler = DataHandler(modelContainer: container)
    try await handler.importLargeDataset(data)
}

// Factory method alternative
@ModelActor
actor DataService {
    static nonisolated func createBackground(container: ModelContainer) async -> DataService {
        await Task.detached { DataService(modelContainer: container) }.value
    }
}
```

## Critical Anti-Patterns

### Passing Model Objects Between Actors

```swift
// BAD: Model objects are not Sendable
func getItem(id: UUID) throws -> Item {  // Crash or undefined behavior
    try modelContext.fetch(...).first!
}
let item = try await dataHandler.getItem(id: someId)
item.name = "New"  // Wrong thread!

// GOOD: Return DTO or use identifier
func getItemDTO(id: UUID) throws -> ItemDTO { ... }
func updateItem(id: PersistentIdentifier, name: String) throws { ... }
```

### Creating Actor on Main Thread

```swift
// BAD: Actor runs on main thread
@MainActor
func setup() {
    let handler = DataHandler(modelContainer: container)
}

// GOOD: Create off main actor
func setup() async {
    await Task.detached {
        let handler = DataHandler(modelContainer: container)
    }.value
}
```

### Single Actor Bottleneck

```swift
// BAD: All operations serialize
Task { try await sharedHandler.heavyImport1() }
Task { try await sharedHandler.heavyImport2() }  // Waits for import1!

// GOOD: Separate actors for independent work
Task.detached {
    let handler1 = DataHandler(modelContainer: container)
    try await handler1.heavyImport1()
}
Task.detached {
    let handler2 = DataHandler(modelContainer: container)
    try await handler2.heavyImport2()
}
```

### Modifying After Actor Boundary

```swift
// BAD: Retaining models from background
let items = try await backgroundActor.fetchAllItems()  // [Item]
items[0].name = "Changed"  // CRASH - wrong context!

// GOOD: Use identifiers to load locally
let identifier = try await backgroundActor.getItemIdentifier()
await MainActor.run {
    let item = mainContext.model(for: identifier) as? Item
    item?.name = "Changed"
}
```

### Missing @MainActor on Observable

```swift
// BAD: UI updates may happen off main thread
@Observable
class ViewModel {
    var items: [Item] = []
    func load() async {
        items = await dataService.fetchItems()  // May update from background
    }
}

// GOOD: Explicit main actor isolation
@Observable @MainActor
class ViewModel {
    var items: [Item] = []
}
```

## Review Questions

- [ ] Is @ModelActor used for heavy data operations?
- [ ] Are model objects passed between actors? (They shouldn't be)
- [ ] Is Task.detached used for background actor creation?
- [ ] Is explicit `save()` called in Task contexts?
- [ ] Are ViewModels marked @Observable @MainActor?
- [ ] Are Sendable DTOs used for cross-actor data?
- [ ] Could a single actor become a bottleneck?
- [ ] Are PersistentIdentifiers used for cross-context references?
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: 198kUpdated 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: 198kUpdated 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: 198kUpdated Claude CodeCodexVerifiedInstall