Deepen your understanding by watching the courses!
Core Data Cheat Sheet for Swift iOS Developers
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 contextletfetchRequest=NSFetchRequest(entityName:"MyEntity")do{letfetchedEntities=tryself.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 contextletfetchRequest=NSFetchRequest(entityName:"MyEntity")fetchRequest.fetchLimit=10do{letfetchedEntities=tryself.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 contextletnewEntity=NSEntityDescription.insertNewObjectForEntityForName("MyEntity",inManagedObjectContext:self.managedObjectContext)as!MyEntity// Set propertiesdo{tryself.managedObjectContext.save()}catch{// Do something in response to error condition}
// 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_ entityletpredicate=NSPredicate(format:"objectID == %@",objectIDFromNSManagedObject)letfetchRequest=NSFetchRequest(entityName:"MyEntity")fetchRequest.predicate=predicatedo{letfetchedEntities=tryself.managedObjectContext.executeFetchRequest(fetchRequest)as![MyEntity]fetchedEntities.first?.FirstPropertyToUpdate=NewValuefetchedEntities.first?.SecondPropertyToUpdate=NewValue// ... Update additional properties with new values}catch{// Do something in response to error condition}do{tryself.managedObjectContext.save()}catch{// Do something in response to error condition}
// Assuming type has a reference to managed object contextletpredicate=NSPredicate(format:"MyEntityAttribute == %@","Matching Value")letfetchRequest=NSFetchRequest(entityName:"MyEntity")fetchRequest.predicate=predicatedo{letfetchedEntities=tryself.managedObjectContext.executeFetchRequest(fetchRequest)as![MyEntity]forentityinfetchedEntities{entity.FirstPropertyToUpdate=NewValueentity.SecondPropertyToUpdate=NewValue// ... Update additional properties with new values}}catch{// Do something in response to error condition}do{tryself.managedObjectContext.save()}catch{// Do something in response to error condition}
// 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_ entityletpredicate=NSPredicate(format:"objectID == %@",objectIDFromNSManagedObject)letfetchRequest=NSFetchRequest(entityName:"MyEntity")fetchRequest.predicate=predicatedo{letfetchedEntities=tryself.managedObjectContext.executeFetchRequest(fetchRequest)as![MyEntity]ifletentityToDelete=fetchedEntities.first{self.managedObjectContext.deleteObject(entityToDelete)}}catch{// Do something in response to error condition}do{tryself.managedObjectContext.save()}catch{// Do something in response to error condition}
// Assuming type has a reference to managed object contextletpredicate=NSPredicate(format:"MyEntityAttribute == %@","Matching Value")letfetchRequest=NSFetchRequest(entityName:"MyEntity")fetchRequest.predicate=predicatedo{letfetchedEntities=tryself.managedObjectContext.executeFetchRequest(fetchRequest)as![MyEntity]forentityinfetchedEntities{self.managedObjectContext.deleteObject(entity)}}catch{// Do something in response to error condition}do{tryself.managedObjectContext.save()}catch{// Do something in response to error condition}
Migrate Core Data Model with Automatic Migrations
1
2
3
4
5
6
letmodel=// set up modelletpscOptions=[NSMigratePersistentStoresAutomaticallyOption:true,NSInferMappingModelAutomaticallyOption:true]letpsc=NSPersistentStoreCoordinator(managedObjectModel:model)try!psc.addPersistentStore(ofType:NSSQLiteStoreType,configurationName:nil,at:storeURL,options:pscOptions)