Back to prompts
CodingChatGPTClaudeGemini

macOS App with Core Data & CloudKit Sync

Build a macOS app with persistent local storage and seamless iCloud sync using Core Data with CloudKit or SwiftData.

Prompt Template

You are a senior Apple developer specializing in data persistence and cloud sync for macOS apps. Help me implement persistent storage with iCloud sync.

**App Description:** [what the app does]
**Data Models:** [describe your entities and relationships, e.g., "Projects have many Tasks, Tasks have Tags (many-to-many)"]
**Sync Requirements:**
- [e.g., "Sync across Mac + iPhone + iPad"]
- [e.g., "Offline-first — must work without internet"]
- [e.g., "Conflict resolution: last-write-wins vs. merge"]
**Data Volume:** [rough estimate — hundreds vs. thousands vs. millions of records]
**Persistence Framework Preference:** [SwiftData (macOS 14+) / Core Data / No preference]
**Sharing:** [private only / shared with other users via CloudKit sharing]

Please provide:
1. **Framework Recommendation** — SwiftData vs. Core Data + CloudKit with trade-offs
2. **Data Model** — complete model definitions with relationships, indexes, and CloudKit-compatible types
3. **Container Setup** — NSPersistentCloudKitContainer or SwiftData ModelContainer configuration
4. **CRUD Operations** — create, read, update, delete with proper save handling
5. **Sync Configuration** — CloudKit container setup, entitlements, capabilities
6. **Conflict Handling** — strategy for merge conflicts
7. **Migration Strategy** — lightweight migration setup for future schema changes
8. **Error Handling** — CloudKit quota errors, network issues, account status
9. **Testing** — how to test sync locally and with TestFlight
10. **Performance** — batch operations, fetch request optimization, fault handling

Example Output

Recommendation: SwiftData + CloudKit (macOS 14+)

For a new app targeting macOS 14+, SwiftData with CloudKit is the simplest path.

Model Definitions

import SwiftData

@Model

class Project {

var name: String

var createdAt: Date

@Relationship(deleteRule: .cascade, inverse: \\Task.project)

var tasks: [Task] = []

init(name: String) {

self.name = name

self.createdAt = .now

}

}

@Model

class Task {

var title: String

var isComplete: Bool = false

var dueDate: Date?

var project: Project?

var tags: [Tag] = []

}

@Model

class Tag {

#Unique<Tag>([\\Tag.name])

var name: String

var tasks: [Task] = []

}

Container Setup

@main

struct MyApp: App {

var container: ModelContainer = {

let schema = Schema([Project.self, Task.self, Tag.self])

let config = ModelConfiguration(

cloudKitDatabase: .automatic

)

return try! ModelContainer(for: schema, configurations: [config])

}()

var body: some Scene {

WindowGroup { ContentView() }

.modelContainer(container)

}

}

Required Entitlements

<key>com.apple.developer.icloud-container-identifiers</key>

<array><string>iCloud.com.yourcompany.appname</string></array>

<key>com.apple.developer.icloud-services</key>

<array><string>CloudKit</string></array>

Tips for Best Results

  • 💡SwiftData with CloudKit requires macOS 14+ — use Core Data if you need to support older versions
  • 💡CloudKit sync only works with iCloud-signed-in users — always handle the signed-out case
  • 💡Unique constraints and CloudKit have quirks — test sync conflicts early
  • 💡Use the CloudKit Dashboard (developer.apple.com) to inspect synced records during development