Use the container that matches your persistence tier: - SwiftData on iOS 17+: configure a `ModelContainer` and keep it on the main actor. - Core Data for older apps: configure an `NSPersistentContainer`. SwiftData example: ```swift import SwiftData @Model final class Note { var title: String init(title: String) { self.title = title } } @MainActor final class PersistenceController { let container: ModelContainer init(inMemory: Bool = false) throws { let config = ModelConfiguration(isStoredInMe...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill ios-persistence --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Ios Persistence?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-ios-persistence-098ecc22)More formats (shields.io, HTML) on the badges page.
Use the container that matches your persistence tier:
- SwiftData on iOS 17+: configure a `ModelContainer` and keep it on the main actor.
- Core Data for older apps: configure an `NSPersistentContainer`.
SwiftData example:
```swift
import SwiftData
@Model
final class Note {
var title: String
init(title: String) {
self.title = title
}
}
@MainActor
final class PersistenceController {
let container: ModelContainer
init(inMemory: Bool = false) throws {
let config = ModelConfiguration(isStoredInMemoryOnly: inMemory)
container = try ModelContainer(for: Note.self, configurations: config)
}
}
```
Core Data example:
```swift
import CoreData
final class PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "MyModel")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores { _, error in
if let error = error {
fatalError("Failed to load store: \(error)")
}
}
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.automaticallyMergesChangesFromParent = true
}
}
```
Good defaults:
- Use SwiftData for new iOS 17+ apps.
- Keep SwiftData container setup on `@MainActor`.
- For Core Data, set an explicit merge policy.
- Do heavy writes in a background context with `newBackgroundContext()`, not on `viewContext`.
- Store secrets like tokens in secure storage, not `UserDefaults`.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!