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;
}

This uses a pathForModelNamed method I ripped from Apple's MigrationV2 sample. It just hunts down the model's .mom file. "MyDocument 3" is the latest version of the model.

The first section does the migration and leaves the original file upgraded. Then, as normal, we call super to actually load the file.

There's no need to override configurePersistentStoreCoordinatorForURL at all. I'll add some code using isConfiguration:compatibleWithStoreMetadata: to check if the upgrade is even needed, and that should wrap it up.

What a day.

Cheers,
Graham Perks.
http://www.asinglepixel.com
_______________________________________________

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