• 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: Graham Perks <email@hidden>
  • Date: Mon, 11 Aug 2008 00:08:12 -0500

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@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 versioning woes
      • From: Quincey Morris <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>)

  • Prev by Date: Re: Why use NSObjectController?
  • Next by Date: Re: Why use NSObjectController?
  • Previous by thread: Re: Core Data versioning woes
  • Next by thread: Re: Core Data versioning woes
  • Index(es):
    • Date
    • Thread