core-data
Skill66 starsChanged 6 months agoMITView on GitHub
Core Data for iOS persistence. Data models, fetch requests, background contexts, and SwiftData migration.
2 other projects use a version of this.
Adopted by
Most recently taken up by
- caglarbaranbora/Apple-Agent-Kittheir own version, first seen Aug 1, 2026
- dpearson2699/swift-ios-skillstheir own version, first seen Jul 13, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- dpearson2699/swift-ios-skills★ 1.2ksame name
- caglarbaranbora/Apple-Agent-Kit★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository51 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rules/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- examples/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/everything-mobile.mdcCursor rule
- accessibility-patternsSkill
- analytics-patternsSkill
- android-patternsSkill
- app-lifecycleSkill
- ci-cd-patternsSkill
- combine-frameworkSkill
- continuous-learningSkill
- continuous-learning-v2Skill
- coroutines-patternsSkill
- deep-linkingSkill
- expect-actualSkill
- feature-builderSkill
- feature-flagsSkill
- gradle-patternsSkill
- image-loadingSkill
- ios-testingSkill
- jetpack-composeSkill
- kmp-diSkill
- kmp-navigationSkill
- kmp-networkingSkill
- kmp-repositoriesSkill
- koin-patternsSkill
- ktor-patternsSkill
- liquid-glassSkill
- localization-patternsSkill
- m3-expressiveSkill
- mobile-checkpointSkill
- mobile-compactionSkill
- mobile-instinct-v1Skill
- mobile-instinct-v2Skill
- mobile-memorySkill
- mobile-securitySkill
- mobile-testingSkill
- mobile-verificationSkill
- mvi-architectureSkill
- navigation-composeSkill
- offline-firstSkill
- pagination-patternsSkill
- push-notificationsSkill
- room-patternsSkill
- shared-coroutinesSkill
- shared-modelsSkill
- sqldelight-patternsSkill
- swift-patternsSkill
- swiftui-patternsSkill
The file
Files
Core Data
iOS persistence with Core Data and SwiftData.
Core Data Stack
Basic Setup
// ✅ Core Data stack
class PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "DataModel")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores { description, error in
if let error = error {
fatalError("Core Data store failed to load: \(error)")
}
}
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
}
var viewContext: NSManagedObjectContext {
container.viewContext
}
func backgroundContext() -> NSManagedObjectContext {
container.newBackgroundContext()
}
}SwiftUI Integration
// ✅ @FetchRequest in SwiftUI
struct UserListView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \User.name, ascending: true)],
animation: .default)
private var users: FetchedResults<User>
var body: some View {
List {
ForEach(users) { user in
Text(user.name ?? "Unknown")
}
.onDelete(perform: deleteUsers)
}
}
private func deleteUsers(offsets: IndexSet) {
withAnimation {
offsets.map { users[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
print("Failed to save: \(error)")
}
}
}
}Fetch Requests
Basic Fetch
// ✅ Simple fetch
func fetchUsers() -> [User] {
let request: NSFetchRequest<User> = User.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
return try viewContext.fetch(request)
} catch {
print("Fetch error: \(error)")
return []
}
}
// ✅ With predicate
func fetchUsers(named name: String) -> [User] {
let request: NSFetchRequest<User> = User.fetchRequest()
request.predicate = NSPredicate(format: "name == %@", name)
request.fetchLimit = 1
do {
return try viewContext.fetch(request)
} catch {
return []
}
}
// ✅ Complex predicate
func searchUsers(query: String) -> [User] {
let request: NSFetchRequest<User> = User.fetchRequest()
request.predicate = NSPredicate(
format: "name CONTAINS[cd] %@ OR email CONTAINS[cd] %@",
query, query
)
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
return try viewContext.fetch(request)
} catch {
return []
}
}Background Context
Background Operations
// ✅ Perform background write
func saveUser(name: String, email: String) {
let context = PersistenceController.shared.backgroundContext()
context.perform {
let user = User(context: context)
user.name = name
user.email = email
user.createdAt = Date()
do {
try context.save()
} catch {
print("Failed to save: \(error)")
}
}
}
// ✅ Batch delete
func deleteAllUsers() {
let context = PersistenceController.shared.backgroundContext()
context.perform {
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = User.fetchRequest()
let batchDelete = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try context.execute(batchDelete)
try context.save()
} catch {
print("Batch delete failed: \(error)")
}
}
}Relationships
Modeling Relationships
// ✅ To-One relationship
extension User {
@NSManaged var department: Department?
}
// ✅ To-Many relationship
extension Department {
@NSManaged var employees: NSSet?
var employeesArray: [User] {
let set = employees as? Set<User> ?? []
return set.sorted { $0.name ?? "" < $1.name ?? "" }
}
}
// ✅ Fetch with relationship
func fetchUsers(in departmentName: String) -> [User] {
let request: NSFetchRequest<User> = User.fetchRequest()
request.predicate = NSPredicate(format: "department.name == %@", departmentName)
do {
return try viewContext.fetch(request)
} catch {
return []
}
}SwiftData (iOS 17+)
Basic SwiftData
// ✅ SwiftData model
@Model
final class User {
var name: String
var email: String
var createdAt: Date
init(name: String, email: String) {
self.name = name
self.email = email
self.createdAt = Date()
}
}
// ✅ SwiftData setup
@main
struct MyApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([User.self])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
// ✅ @Query in SwiftUI
struct UserListView: View {
@Environment(\.modelContext) private var modelContext
@Query(sort: \User.name, order: .forward) private var users: [User]
var body: some View {
List {
ForEach(users) { user in
Text(user.name)
}
.onDelete(perform: deleteUsers)
}
}
private func deleteUsers(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(users[index])
}
}
}
}SwiftData Predicates
// ✅ #Predicate macro
@Query(filter: #Predicate<User> { user in
user.name.contains("John") && user.createdAt > Date().addingTimeInterval(-86400)
})
var recentJohns: [User]
// ✅ Dynamic predicate
struct UserListView: View {
@State private var searchText = ""
var body: some View {
UserList(searchText: searchText)
}
}
struct UserList: View {
let searchText: String
var predicate: Predicate<User> {
#Predicate<User> { user in
user.name.contains(searchText)
}
}
var body: some View {
@Query(filter: predicate) var users: [User]
List(users) { user in
Text(user.name)
}
}
}Migration
Lightweight Migration
let description = NSPersistentStoreDescription(url: storeURL)
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = trueMigration Strategy
// ✅ Custom mapping model
func migrateStore(at sourceURL: URL, to destinationURL: URL) {
let migrationManager = NSMigrationManager(
sourceStoreAt: sourceURL,
destinationStoreAt: destinationURL
)
do {
try migrationManager.migrateStore(
from: sourceURL,
sourceType: NSSQLiteStoreType,
with: nil,
to: destinationURL,
destinationType: NSSQLiteStoreType
)
} catch {
print("Migration failed: \(error)")
}
}Best Practices
// ✅ Use background context for writes
func batchInsert(items: [Item]) {
let context = backgroundContext()
context.perform {
for item in items {
let managedItem = Item(context: context)
managedItem.name = item.name
}
try? context.save()
}
}
// ✅ Batch fetch for large datasets
func fetchAllUsersEfficiently() -> [User] {
let request = NSFetchRequest<User>(entityName: "User")
request.returnsObjectsAsFaults = false
request.fetchBatchSize = 100
return try! viewContext.fetch(request)
}
// ❌ Don't block main thread
// ❌ Bad: Fetching on main thread with large data
let users = viewContext.fetch(largeRequest) // Blocks UI
// ✅ Good: Background fetch
let context = backgroundContext()
context.perform {
let users = try! context.fetch(largeRequest)
DispatchQueue.main.async {
// Update UI with results
}
}Remember: Core Data is powerful but complex. Use it for complex data models, consider SwiftData for new projects.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

