Database Reference
In-Depth Information
5. The migration manager needs to know the connection between the source
object, the newly created destination object and the mapping. Failing to call this
method at the end of a custom migration will result in missing data in the
destination store.
6. Finally, the return value tells the migration manager that everything went well.
That's it for the custom migration code! Core Data will pick up the mapping model
when it detects a v3 data store on launch, and apply it to migrate it to the new data
model version. Since you added the custom
NSEntityMigrationPolicy
subclass and
linked to it in the mapping model, Core Data will call through to your code
automatically.
You just need to wrap up the changes to the new entity subclasses. Open
Attachment.swift
and remove the
image
property.
Next, create a new Swift file called
ImageAttachment
and replace its contents
with the following:
import
UIKit
import
CoreData
class
ImageAttachment:
Attachment
{
@NSManaged
var
image:
UIImage
?
@NSManaged
var
width:
CGFloat
@NSManaged
var
height:
CGFloat
@NSManaged
var
caption:
NSString
}
There are several places in the app that reference
Attachment
that you now need to
change to reference
ImageAttachment
.
Open
Note.swift
and change the first two lines of
latestAttachment()
so that they
reference
ImageAttachment
instead:
func
latestAttachment() ->
ImageAttachment
? {
var
attachmentsToSort =
attachments
.
allObjects
as
[
ImageAttachment
]
Finally, open
AttachPhotoViewController.swift
and find
imagePickerController(_:didFinishPickingMediaWithInfo:)
. Change the line that
sets up
attachment
so that it uses
ImageAttachment
instead:
let
attachment =
NSEntityDescription
.
insertNewObjectForEntityForName
(
"ImageAttachment"
,
inManagedObjectContext: note.
managedObjectContext
)
as
ImageAttachment