Updated on July 27, 2016 – Additional Cheats
Having trouble recalling how to perform basic Core Data operations? This cheat sheet is a handy reference to keep you productive with Core Data and Swift!
The code snippets below are here to help jog your memory when it’s been a while since you’ve worked in Core Data. They could also be helpful for newcomers to iOS development, Core Data, and Swift.
One assumption I’m making in this post is that you’ve created NSManagedObject subclasses for your entities to make them easier to work with in a type-safe way. If you need help getting started with that, I’ve written a walk-through to guide you through that process.
Querying
Fetch all entities
1
2
3
4
5
6
7
8
9
|
// Assuming type has a reference to managed object context
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
// Do something with fetchedEntities
} catch {
// Do something in response to error condition
}
|
Fetch maximum of N entities
1
2
3
4
5
6
7
8
9
10
|
// Assuming type has a reference to managed object context
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
fetchRequest.fetchLimit = 10
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
// Do something with fetchedEntities
} catch {
// Do something in response to error condition
}
|
Insert a new entity
1
2
3
4
5
6
7
8
9
10
|
// Assuming encapsulating Type has a reference to managed object context
let newEntity = NSEntityDescription.insertNewObjectForEntityForName("MyEntity", inManagedObjectContext: self.managedObjectContext) as! MyEntity
// Set properties
do {
try self.managedObjectContext.save()
} catch {
// Do something in response to error condition
}
|
Update a single entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// Assuming type has a reference to managed object context
// Assuming that a specific NSManagedObject's objectID property is accessible
// Alternatively, could supply a predicate expression that's precise enough
// to select only a _single_ entity
let predicate = NSPredicate(format: "objectID == %@", objectIDFromNSManagedObject)
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
fetchRequest.predicate = predicate
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
fetchedEntities.first?.FirstPropertyToUpdate = NewValue
fetchedEntities.first?.SecondPropertyToUpdate = NewValue
// ... Update additional properties with new values
} catch {
// Do something in response to error condition
}
do {
try self.managedObjectContext.save()
} catch {
// Do something in response to error condition
}
|
Update multiple-entities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// Assuming type has a reference to managed object context
let predicate = NSPredicate(format: "MyEntityAttribute == %@", "Matching Value")
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
fetchRequest.predicate = predicate
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
for entity in fetchedEntities {
entity.FirstPropertyToUpdate = NewValue
entity.SecondPropertyToUpdate = NewValue
// ... Update additional properties with new values
}
} catch {
// Do something in response to error condition
}
do {
try self.managedObjectContext.save()
} catch {
// Do something in response to error condition
}
|
Delete a single entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// Assuming type has a reference to managed object context
// Assuming that a specific NSManagedObject's objectID property is accessible
// Alternatively, could supply a predicate expression that's precise enough
// to select only a _single_ entity
let predicate = NSPredicate(format: "objectID == %@", objectIDFromNSManagedObject)
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
fetchRequest.predicate = predicate
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
if let entityToDelete = fetchedEntities.first {
self.managedObjectContext.deleteObject(entityToDelete)
}
} catch {
// Do something in response to error condition
}
do {
try self.managedObjectContext.save()
} catch {
// Do something in response to error condition
}
|
Delete multiple-entities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// Assuming type has a reference to managed object context
let predicate = NSPredicate(format: "MyEntityAttribute == %@", "Matching Value")
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
fetchRequest.predicate = predicate
do {
let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [MyEntity]
for entity in fetchedEntities {
self.managedObjectContext.deleteObject(entity)
}
} catch {
// Do something in response to error condition
}
do {
try self.managedObjectContext.save()
} catch {
// Do something in response to error condition
}
|
Migrate Core Data Model with Automatic Migrations
1
2
3
4
5
6
|
let model = // set up model
let pscOptions = [NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true]
let psc = NSPersistentStoreCoordinator(managedObjectModel: model)
try! psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: pscOptions)
|