• 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 versioning woes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core Data versioning woes


  • Subject: Re: Core Data versioning woes
  • From: Quincey Morris <email@hidden>
  • Date: Sun, 10 Aug 2008 23:21:09 -0700

On Aug 10, 2008, at 22:08, Graham Perks wrote:

Well that was too easy. I even started off on the right track yesterday before getting derailed.

This'll work:

- (id)initWithContentsOfURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
// Migrate? Optional, but it'd be good to check here if this upgrade needs to happen.


NSError *error = nil;
NSURL *momURL = [NSURL fileURLWithPath:[MyDocument pathForModelNamed:@"MyDocument 3"]];
NSManagedObjectModel *newMoM = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:newMoM];
NSDictionary *optionsDictionary =
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];


   id store = [psc addPersistentStoreWithType:NSSQLiteStoreType
                                configuration:nil
                                          URL:absoluteURL
                                      options:optionsDictionary
                                        error:&error];

   if (nil == store) {
       [[NSApplication sharedApplication] presentError:error];
   }

   // Migration has happened.
   [psc release];
   [newMoM release];

self = [super initWithContentsOfURL:absoluteURL ofType:typeName error:outError];

   return self;
}

Er, I think this is a little bit wrong, because it doesn't handle errors properly. If you get an error from addPersistentStoreWithType, you'll report it, then get a secondary error from [super initWithContentsOfURL:...]. And you can't return early if an error occurs, because this is an initializer and returning early (without calling a designated super initializer) will return an uninitialized MyDocument object which *may* crash your application. Nor can you return nil -- the NSDocument class reference says it's not allowed.


It's possible that simply calling [super init] before returning self, after an error, will make this safe, but I still think you're better off doing the migration in a subclass of NSDocumentController, before even trying to create the MyDocument object. (The identical migration code will work there, because it doesn't depend on a MyDocument instance.) -[NSDocumentController openDocumentWithContentsOfURL:display:error:] is the method you'd need to override.

The other consideration to keep in mind is that automation migration (NSMigratePersistentStoresAutomaticallyOption) is probably going to end up keeping the whole persistent store in memory (twice -- the old one and the new one -- plus the property data caches that core data uses). If your stores are more than a few hundred megabytes, this might perform *really* badly.


_______________________________________________

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


References: 
 >Core Data versioning woes (From: Graham Perks <email@hidden>)
 >Re: Core Data versioning woes (From: Graham Perks <email@hidden>)
 >Re: Core Data versioning woes (From: Graham Perks <email@hidden>)
 >Re: Core Data versioning woes (From: Graham Perks <email@hidden>)
 >Re: Core Data versioning woes (From: Graham Perks <email@hidden>)
 >Re: Core Data versioning woes (From: Graham Perks <email@hidden>)

  • Prev by Date: Re: Why use NSObjectController?
  • Next by Date: How increase the default http request timeout?
  • Previous by thread: Re: Core Data versioning woes
  • Next by thread: Re: Core Data versioning woes
  • Index(es):
    • Date
    • Thread