Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration issues with UIManagedDocument

I started using CoreData in my application following Stanford CS193P lessons regarding the use of iOS 5's new class UIManagedDocument. The approach itself is quite straightforward but I can't understand how to deal with model modifications i keep making. This is how I instantiate my UIManagedDocument object (inside the appDelegate, so that every other class can use it):

if (!self.database) {
    NSURL *url=[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"AppName"];

    UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:url];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    doc.persistentStoreOptions = options;
    self.database=doc;
    [doc release];
}

The issue I have is that every time I change even a little bit of my .xcdatamodel, I am unable to get all the content previously stored in the document as well as to create any new instance. As a matter of fact doing this generates the following exception:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

I thought setting the "options" property of the managed document would have solved the problem, but apparently this isn't enough. Anyone can help? Couldn't' find other questions that actually fit my precise needs.

like image 600
micamoita Avatar asked Mar 19 '12 13:03

micamoita


1 Answers

Before you modify your Core Data model, you should "Add Model Version".

1. Select the original model file. (e.g. YourProject.xcdatamodel)

2. "Editor" -> "Add Model Version...". Then add a new model version (e.g. 2.0)

3. You will get a new model file. (e.g. YourProject 2.0.xcdatamodel). Modify it.

4. Change the current model version. Select the top .xcdtatmodel file -> "View" -> "Utilities" -> "Show File Inspector". Find the tag "Versioned Core Data Model" and choose the right version you want to modify.

It also disturbed me for a long long time. Hope this way can help you ^^

like image 111
ananfang Avatar answered Oct 19 '22 20:10

ananfang