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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to