On 2009 Nov 19, at 14:05, Matthew Lindfield Seager wrote:

> I can't help with your question but Googling
> "NSMigratePersistentStoresAutomaticallyOption site:lists.apple.com"
> yields 36 results. There's probably several duplicates but it give you
> a head start....

Hmmm.  When I tried that this morning I only got 19.  Among the additional 17, 
I found this:

http://lists.apple.com/archives/cocoa-dev/2008/Aug/msg00767.html

Summary: There is no need to muck with NSPersistentDocument's built-in and 
bug-free Persistence Stack.  Happy Day!  All you do is override 
-[NSPersistentDocument initWithContentsOfURL:ofType:error:].  I submitted 
document feedback suggesting this to the Core Data Model Versioning and Data 
Migration Programming Guide.

Thanks, Matthew.

The code in that post had issues and didn't quite work, so I fixed it up and it 
now seems to "just work" now for all three cases (1) oldest docs saved with old 
model (2) old docs with new model (3) new docs.  In case (1) the old file 
remains with a tilde suffix, as expected.  In case (3), the method doesn't even 
run.  But when it does, it handles errors properly :)

So, the answer to the question: How to enable Default Automatic Migration in a 
Core Data document-based app, is...

Simply drop the following override into the NSPersistentDocument subclass:

- (id)initWithContentsOfURL:(NSURL*)url
                     ofType:(NSString*)typeName
                      error:(NSError**)error_p {
    // NSPersistentDocument's built-in Persistence Stack seems to create a store
    // without setting the NSMigratePersistentStoresAutomaticallyOption, so
    // automatic migration just "doesn't work".  The following code, which runs
    // only for existing documents, works around that problem.
    NSError* error = nil ;
    NSArray* bundles = [NSArray arrayWithObject:[NSBundle mainBundle]] ;
    NSManagedObjectModel* mergedMOM = [NSManagedObjectModel 
mergedModelFromBundles:bundles] ;
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] 
initWithManagedObjectModel:mergedMOM];
    NSDictionary *optionsDictionary =
    [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                
forKey:NSMigratePersistentStoresAutomaticallyOption];
    
    id store = [psc addPersistentStoreWithType:NSSQLiteStoreType
                                 configuration:nil
                                           URL:url
                                       options:optionsDictionary
                                         error:&error] ;
    // If migrating the document from and older to the current model was 
necessary,
    // it is now all done!  The addPersistentStoreWithType::::: will have 
renamed the
    // old file with a tilde on the end and created a new, migrated file at the 
given
    // URL.  So now, when we invoke super, it will open the new file and just 
work.
    // Very nice.

    if (store) {
        self = [super initWithContentsOfURL:url
                                     ofType:typeName
                                      error:&error] ;
        // The above method will return nil if it fails.
    }
    else {
        [self release] ;
        self = nil ;
    }
    
    if (!self && error_p) {
        *error_p = error ;
    }
    
    return self;
}

_______________________________________________

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