Migration issues with UIManagedDocument
Asked Answered
T

3

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.

Tropical answered 19/3, 2012 at 13:24 Comment(1)
I followed Stanford CS193P too, and I am experiencing the same error.Hast
J
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 ^^

Jonijonie answered 2/4, 2012 at 7:23 Comment(3)
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 but it doesn't use the samy UIManagedDocument approach as I need...Tropical
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.Counterstamp
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).Counterstamp
J
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.

Jilly answered 16/7, 2012 at 17:56 Comment(3)
and what if the app is already in the store. you display an alert view and ask users to reinstall your application?Heulandite
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:)Cyton
this didn't help me. Persistent store is not available it says, but I clearly have it!Hast
H
0

Make sure that you did not mistype NSDocumentDirectory as NSDocumentationDirectory.

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

NSDocumentDirectory is right!

Hast answered 17/7, 2013 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.