Re: Core Data migration: how to delete some managed objects?
Re: Core Data migration: how to delete some managed objects?
- Subject: Re: Core Data migration: how to delete some managed objects?
- From: Jerry Krinock <email@hidden>
- Date: Mon, 04 Mar 2013 22:17:44 -0800
On 2013 Mar 04, at 14:38, Sean McBride <email@hidden> wrote:
> What's a good way to delete some managed objects from a store when migrating the store from version x to version y? Basically, for entity 'Foo', I'd like for instances that match predicate 'bar' to not exist in the new store.
This is easy to do with a mapping model, migration policy and migration manager. In your NSMigrationManager subclass implementation, implement an override something like thisā¦
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject*)oldInstance
entityMapping:(NSEntityMapping*)inMapping
manager:(NSMigrationManager*)inManager
error:(NSError**)error_p {
BOOL ok = YES ;
NSError* error = nil ;
NSEntityDescription *sourceInstanceEntity = [oldInstance entity] ;
NSString* sourceInstanceEntityName = [sourceInstanceEntity name] ;
if ([sourceInstanceEntityName isEqualToString:constEntityNameStark]) {
// Here's your predicateā¦
if ([oldInstance valueForKey:constKeyWhatever] passes the predicate) {
ok = [super createDestinationInstancesForSourceInstance:oldInstance
entityMapping:inMapping
manager:inManager
error:&error] ;
}
else {
// Don't want this object in the new store. Do nothing!
}
}
if (error && error_p) {
*error_p = error ;
}
return ok ;
}
You can tweak destination instances by getting with -destinationInstancesForEntityMappingNamed:sourceInstances:. But be careful to send only NSManagedObject messages to the source and destination instances. Although you think of them as your old MyBazManagedObject, remember they're not. Don't use -myProperty. Use -valueForKey:@"myProperty".
You can literally re-make your whole data model by subclassing NSMigrationManager. Merge entities, make new relationships, whatever your marketing department desires :)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden