Persistence setup

From rshankras/claude-code-apple-skills · View interactive version

Persistence setup is an AI agent skill from the GitHub repository rshankras/claude-code-apple-skills. Generates SwiftData or CoreData persistence layer with optional iCloud sync. Use when user wants to add local storage, data persistence, or cloud sync.

How to use this skill

Skill file

Persistence Setup Generator

Generates a production-ready persistence layer using SwiftData (iOS 17+) or CoreData with optional iCloud (CloudKit) sync.

When This Skill Activates

Pre-Generation Checks (CRITICAL)

1. Project Context Detection

Before generating, ALWAYS check:

# Check deployment target
cat Package.swift | grep -i "platform"
# Or check project.pbxproj

# Find existing persistence implementations
rg -l "ModelContainer|NSPersistentContainer|@Model|@Entity" --type swift

# Check for existing SwiftData models
rg "@Model" --type swift | head -5

# Check for CoreData stack
rg "NSManagedObjectContext|NSPersistentStore" --type swift | head -5

# Check existing entitlements for iCloud
cat *.entitlements 2>/dev/null | grep -i "icloud"

2. Framework Selection

Use SwiftData if:

Use CoreData if:

3. Conflict Detection

If existing persistence found:

Configuration Questions

Ask user via AskUserQuestion:

  1. Framework choice?
  1. Enable iCloud sync?
  1. Generate example model?

Generation Process

Step 1: Create Core Files

Always generate:

Sources/Persistence/
├── PersistenceController.swift    # Container setup
├── Repository.swift               # Repository protocol
└── SwiftDataRepository.swift      # Concrete implementation

If example model requested:

Sources/Persistence/Models/
└── Item.swift                     # Sample @Model

If iCloud enabled:

Sources/Persistence/CloudSync/
├── CloudKitConfiguration.swift    # Container identifier
└── SyncStatus.swift               # Sync monitoring

Step 2: Read Templates

Read templates from this skill:

Step 3: Customize for Project

Adapt templates to match:

Step 4: Integration

Basic Integration:

@main
struct MyApp: App {
    let container = PersistenceController.shared.container

    var body: some Scene {
        WindowGroup {
            ContentView()
                .modelContainer(container)
        }
    }
}

With iCloud sync:

@main
struct MyApp: App {
    let container = PersistenceController.shared.container

    var body: some Scene {
        WindowGroup {
            ContentView()
                .modelContainer(container)
                .environment(\.syncStatus, SyncStatus.shared)
        }
    }
}

iCloud Sync Setup

Required Capabilities (Xcode)

  1. iCloud capability:
  1. Background Modes (optional, for background sync):

Required Entitlements

<key>com.apple.developer.icloud-container-identifiers</key>
<array>
    <string>iCloud.com.yourcompany.yourapp</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
    <string>CloudKit</string>
</array>

CloudKit Dashboard Setup

  1. Go to CloudKit Dashboard
  2. Select your container
  3. Schema is auto-created from @Model classes
  4. Deploy schema to production before release

Generated Code Patterns

Repository Protocol

protocol Repository<T>: Sendable {
    associatedtype T: PersistentModel

    func fetch(predicate: Predicate<T>?, sortBy: [SortDescriptor<T>]) async throws -> [T]
    func insert(_ item: T) async throws
    func delete(_ item: T) async throws
    func save() async throws
}

SwiftData Model

@Model
final class Item {
    var title: String
    var timestamp: Date
    var isCompleted: Bool

    init(title: String, timestamp: Date = .now, isCompleted: Bool = false) {
        self.title = title
        self.timestamp = timestamp
        self.isCompleted = isCompleted
    }
}

Container with CloudKit

let container = try ModelContainer(
    for: Item.self,
    configurations: ModelConfiguration(
        cloudKitDatabase: .private("iCloud.com.yourcompany.yourapp")
    )
)

Verification Checklist

After generation, verify:

Common Customizations

Adding New Models

@Model
final class Project {
    var name: String
    @Relationship(deleteRule: .cascade) var items: [Item]

    init(name: String, items: [Item] = []) {
        self.name = name
        self.items = items
    }
}

// Update container
let container = try ModelContainer(for: Item.self, Project.self)

Custom Fetch Descriptors

let descriptor = FetchDescriptor<Item>(
    predicate: #Predicate { $0.isCompleted == false },
    sortBy: [SortDescriptor(\.timestamp, order: .reverse)]
)
let items = try modelContext.fetch(descriptor)

Migration (SwiftData)

// SwiftData handles lightweight migrations automatically
// For complex migrations, use VersionedSchema

enum ItemSchemaV1: VersionedSchema {
    static var versionIdentifier = Schema.Version(1, 0, 0)
    static var models: [any PersistentModel.Type] { [Item.self] }
}

Troubleshooting

iCloud Sync Not Working

  1. Check entitlements match CloudKit container
  2. Verify CloudKit Dashboard shows your container
  3. Check device is signed into iCloud
  4. Deploy schema to production if testing on release build

Data Not Persisting

  1. Verify modelContainer modifier is on root view
  2. Check save() is called after modifications
  3. Look for errors in Console.app

CloudKit Quota Exceeded

Related Skills

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.