6

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.

micamoita
  • 450
  • 2
  • 14

3 Answers3

2

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 ^^

ananfang
  • 566
  • 1
  • 8
  • 19
  • Hi. Thank you for the answer. Seems like the good way to go. Anyway, after following your suggestion, I keep getting migration issues: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't merge models with two different entities named 'ManagedChild'' where ManagedChild is one of the entities in my model. I found this [entry](http://stackoverflow.com/questions/3205783/core-data-error-cant-merge-models-with-two-different-entities-named-foo) but it doesn't use the samy UIManagedDocument approach as I need... – micamoita Apr 02 '12 at 10:15
  • I'm getting the same problem. All the documentation says that adding the "persistentStoreOptions" is the way to go. But I get a nil return from UIManagedDocument along with the same error messages. Deleting the store is not a valid option for me, since I'm supporting a released app. – Danny Sung May 09 '14 at 02:50
  • Just another data point... I created a sample project that's initializing Core Data via UIManagedDocument exactly the same way I'm doing in my app, but the sample project seems to migrate just fine (even without any persistentStoreOptions). – Danny Sung May 09 '14 at 05:53
0

There is a really simple solution to your problem. Simply erase the app from the simulator or device (touch the app icon for a few seconds, and touch the cross that appears when the app icons start wiggling). That way, Xcode updates your app's UIManagedDocument.

pH-k
  • 39
  • 2
  • 1
    and what if the app is already in the store. you display an alert view and ask users to reinstall your application? – MrBr Sep 04 '12 at 09:51
  • I think pH-k's answer is what the questioner needs since he is following a tutorial, not for publishing an app to the store:) – Philip007 Nov 15 '12 at 07:05
  • this didn't help me. Persistent store is not available it says, but I clearly have it! – coolcool1994 Jul 02 '13 at 15:01
0

Make sure that you did not mistype NSDocumentDirectory as NSDocumentationDirectory.

   NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

NSDocumentDirectory is right!

coolcool1994
  • 3,704
  • 4
  • 39
  • 43