Database Reference
In-Depth Information
newAttachment. setValue (destinationValue,
forKey: destinationName)
}
}
// 3
if let image = sInstance. valueForKey ( "image" ) as ? UIImage {
newAttachment. setValue (image. size . width , forKey: "width" )
newAttachment. setValue (image. size . height , forKey: "height" )
}
// 4
let body = sInstance. valueForKeyPath ( "note.body" ) as NSString
newAttachment. setValue (body. substringToIndex ( 80 ),
forKey: "caption" )
// 5
manager. associateSourceInstance (sInstance,
withDestinationInstance: newAttachment,
forEntityMapping: mapping)
// 6
return true
}
This method is an override of the default NSEntityMigrationPolicy implementation.
Overriding this method is the way the migration manager creates instances of
destination entities. That is, an instance of the source object is the first parameter,
and, when overridden, it's up to the developer to create the destination instance
and associate it properly to the migration manager.
Here's what's going on, step by step:
1. First, you create an instance of the new destination object. The migration
manager has two Core Data stacks—one to read from the source and one to write
to the destination—so you need to be sure to use the destination context here.
2. Even though this is a custom manual migration, most of the attribute migrations
should be performed using the expressions you defined in the mapping model. To
do this, you loop through each attribute mapping from the entityMapping
parameter, apply the value expression to the source instance and set the result to
the new destination object.
3. Next, you try to get an instance of the image. If it exists, then you grab its width
and height to populate the data in the new object.
4. For the caption, you simply grab the note's body text and take the first 80
characters.
Search WWH ::




Custom Search