Sample project to work with CoreData and managed objects.
Setup new project.
- Xcode 7.3
- IOS 9
- Swift 2.2
Create new project File > New > Project
Project details - add the following
- Product Name MovieWorld
- Language Swift
- Device Universal
- DO NOT SELECT Use Core Data (we will be creating a helper class)
Save and add version control
Setup CoreData We will be creating a helper class to access the managed object context which is used to access CoreData. We will also be setting up the CoreData model which is use to create the mapping to the database.
- Create CoreDataStack class Swift file
- File > New > File
- select iOS Source and Swift
- Name CoreDataStack
- import CoreData
import CoreData class CoreDataStack { }
- create constant variable "MovieWorld"
class CoreDataStack { let modelName = "MovieWorld"
- managed object model (loads the data model named "MovieWord")
private lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource(self.modelName, withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! }()
- application document dir
private lazy var applicationDocumentsDirectory: NSURL = { let urls = NSFileManager .defaultManager() .URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) return urls.last! }()
- persistent store coordinator - coordinates the objects between the data store and the managed context. Also verifies the data is in a consistent state.
private lazy var psc: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(self.modelName) print("Persistent Store URL: \(url)") do { let options = [NSMigratePersistentStoresAutomaticallyOption : true] try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options) } catch { print("Error adding persistent store.") } return coordinator }()
- managed object context - This is the context (object) the application works with to manipulate data. It is usually described as a "scratch pad". The objects are pulled from the persistent store and placed in the memory in the context where they may be changed. If the changes are not saved, the persistent store remains unchanged. All objects must be registered with the context so any new objects will need to be registered.
lazy var context: NSManagedObjectContext = { var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) managedObjectContext.persistentStoreCoordinator = self.psc managedObjectContext.name = "Main Context" managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy return managedObjectContext }()
- Create the CoreData Model - This is used to make the mapping to the data store and the relations between the objects and much more.
- File > New > iOS CoreData and select Data Model
- Select Next and name MovieWorld and save (this matches the model name above)
- Instantiate CoreDataStack and print context name
- Open AppDelegate.swift and delete all functions except didFinishLaunchingWithOptions
- Instantiate CoreData and print the context name
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let coreDataStack = CoreDataStack() print("Context Name: \(coreDataStack.context.name!)") return true }
- run app and view console and verify the path to the database and the context name is printed out
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let coreDataStack = CoreDataStack() print("Context Name: \(coreDataStack.context.name!)") return true }
Define the data model.
NOTE: make change to the the .xcdatamodeld requires a migration. We will not tackle that sine we are not deploying code. Just remember to reset the the simulator (Simulator > Reset Content & Settings) or delete and reinstall the application.
We will be creating the following data model:
Open the MovieWorld data model MovieWorld.xcdatamodeld
Add the following entities
Table | Attribute | Type |
---|---|---|
Movie | duration | Int32 |
title | String | |
Rating | descr | String |
code | String | |
Genre | name | String |
Add relationships
relationship | Destination | Inverse | Type |
---|---|---|---|
genre | Genre | movies | To One |
rating | Rating | movies | To One |
relationship | Destination | Inverse | Type |
---|---|---|---|
movies | Movie | genre | To Many |
relationship | Destination | Inverse | Type |
---|---|---|---|
movies | Movie | rating | To Many |
Clear the simulator cache or delete the app and reinstall since we have not setup a migration.
Simulator > Reset Content and Settings
Build and Run