Database Reference
In-Depth Information
import CoreData
Replace the image picker delegate method with the following:
func imagePickerController(picker: UIImagePickerController ,
didFinishPickingMediaWithInfo info: NSDictionary ) {
if let note = note {
let attachment =
NSEntityDescription . insertNewObjectForEntityForName ( "Attachment" ,
inManagedObjectContext: note . managedObjectContext ) as Attachment
attachment. dateCreated = NSDate ()
attachment. image = info[ UIImagePickerControllerOriginalImage ] as?
UIImage
attachment. note = note
}
navigationController ? . popViewControllerAnimated ( true )
}
Build and run the app, and you'll notice that not a whole lot has changed on the
surface! However, if you still see your notes and images as before, that means the
mapping model worked; under the covers, Core Data has updated the underlying
schema of the SQLite store to reflect the changes in the v3 data model.
Note : Again, you might want to make a copy of the v3 source code into a
different folder to come back to later. Or if you're using source control, set a
tag here so you can come back to this point.
A complex mapping model
The higher-ups have thought of a new feature for UnCloudNotes, so you know what
that means: It's time to migrate the data model once again! This time, they've
decided that supporting only image attachments isn't enough. They want future
versions of the app to support videos, audio files or really add any kind of
attachment that makes sense.
You make the decision to have a base entity called Attachment and a subclass
called ImageAttachment . This will enable each attachment type to have its own
useful information. Images could have attributes for a caption, image size,
compression level, file size, et cetera. Later, you can add more subclasses for other
attachment types.
While new images will grab this information prior to saving, you'll need to extract
that information from current images during the migration. You'll need to use either
CoreImage or the ImageIO libraries. These are data transformations that Core Data
 
Search WWH ::




Custom Search