Database Reference
In-Depth Information
So really, the mapping model is calling a method on the $manager object. The
$manager token is a special reference to the NSMigrationManager object that is
handling the migration process.
Note : Using FUNCTION expressions still relies on some knowledge of Objective-
C syntax. It might be some time until Apple gets around to revamping Core
Data 100% to Swift! :]
Core Data creates the migration manager during the migration. The migration
manager keeps track of which source objects are associated with which destination
objects. The method destinationInstancesForEntityMappingNamed:sourceInstances:
will look up the destination instances for a source object. The expression above is
saying “set the note relationship to whatever the $source object for this mapping
gets migrated to by the NoteToNote mapping,” which in this case will be the Note
entity in the new data store.
You've completed your custom mapping! You now have a mapping that is
configured to split a single entity into two and relate the proper data objects
together.
Persistent store and entity classes
Before running this migration, you need to update the Core Data setup code to use
this mapping model and not try to infer one on its own. Open
NotesListViewController.swift and find the CoreDataStack property initializer
near the top of the class. Change the options dictionary to match the following:
options:[NSMigratePersistentStoresAutomaticallyOption: true ,
NSInferMappingModelAutomaticallyOption: false ]
By setting NSInferMappingModelAutomaticallyOption to false , you've ensured that
the persistent store coordinator will now use the new mapping model to migrate the
store. Yes, that's all the code you need to change; there is no new code!
What will happen here is that Core Data will look for the mapping model files in the
default or main bundle when it's told not to infer or generate one. Since the
mapping model knows the source and destination versions of the model, Core Data
will use that information to determine which mapping model to use to perform a
migration. It really is as simple as changing a single option to use the custom
mapping model.
The code in the app also needs updating—you're not working with the image
property on a note any more, but with multiple attachments.
Create a new Swift file called Attachment and implement the managed object
subclass:
Search WWH ::




Custom Search