Database Reference
In-Depth Information
The new model is now ready for some code! Open
Note.swift
and add a property
to match the new attribute:
@NSManaged
var
image:
UIImage
?
Build and run, and you'll see your notes have disappeared! In the Xcode console,
you'll see some error text related to the
CoreDataStack
object:
context:(<NSManagedObjectContext:(0x7a96c8f0>(
modelName:(UnCloudNotesDataModelmodel:([Note:(<c393d6c8(d5e65f5a(bb5e4394(
eb6ce54c(a99e724d(bdb64072(ed2e99dd(99e77ba0>](
coordinator:(<NSPersistentStoreCoordinator:(0x7a973310>(
storeURL:(
file:///Users/YOURNAME/Library/Developer/CoreSimulator/Devices/A24A4E68TD616T
4F63T8946T652164EE5E53/data/Containers/Data/Application/9921B2DDTD0FDT4330T
90F9TA2F44CC9899A/Library/Application%20Support/UnCloudNotes.sqlite(
store
:'nil
The store file is still around (
storeURL
in the log above), but since it's incompatible
with the new v2 model, Core Data couldn't attach it to the persistent store, so
store
is still
nil
.
Core Data can automatically update your store if all you've done is add a new
property like this. These are called
lightweight migrations
.
Enabling lightweight migrations
To enable lightweight migrations, you need to set two flags on initialization. The
stack in this app lives in an object imaginatively titled
CoreDataStack
, which you'll
modify to do this.
Open
CoreDataStack.swift
and add a property to the class:
var
options:
NSDictionary
?
Right now, you're setting up the persistent store with no options for default
behavior. You'll use the
options
dictionary to set the necessary flags.
Next, update the initializer to match the following: