• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Core Data migration: how to delete some managed objects?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Core Data migration: how to delete some managed objects?
      • From: Sean McBride <email@hidden>
    • Re: Core Data migration: how to delete some managed objects?
      • From: Jerry Krinock <email@hidden>
References: 
 >Core Data migration: how to delete some managed objects? (From: Sean McBride <email@hidden>)

  • Prev by Date: Re: getting ivar of fundamental types with object_getInstanceValue
  • Next by Date: Re: Core Data migration: how to delete some managed objects?
  • Previous by thread: Core Data migration: how to delete some managed objects?
  • Next by thread: Re: Core Data migration: how to delete some managed objects?
  • Index(es):
    • Date
    • Thread