1

edit

I have reposted this here: Core Data & UIManagedDocument in Xcode 4.3: Can't merge models

I managed to get a different error to show.

end of edit

so I am making a Core Data App that used a shared UIManagedDocument. I access this UIManagedDocument using a helper class DocumentHandler. The hope is that I'll be able to use it like this in another class's viewWillAppear:

if (!self.document) {
        [[DocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
            self.document = document;            
            [self setupFetchedResultsController];
        }]; 
    }

`

- (id)init {
    self = [super init];
    if (self) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"MyJimDocument"];

        self.document = [[UIManagedDocument alloc] initWithFileURL:url];

        // Set our document up for automatic migrations
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
        self.document.persistentStoreOptions = options;

        // Register for notifications
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(objectsDidChange:)
                                                     name:NSManagedObjectContextObjectsDidChangeNotification
                                                   object:self.document.managedObjectContext];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(contextDidSave:)
                                                     name:NSManagedObjectContextDidSaveNotification
                                                   object:self.document.managedObjectContext];
    }
    return self;
}


- (void)performWithDocument:(OnDocumentReady)onDocumentReady {    
    void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) {
        onDocumentReady(self.document);
    };

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) {
        [self.document saveToURL:self.document.fileURL
                forSaveOperation:UIDocumentSaveForCreating
               completionHandler:OnDocumentDidLoad];
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:OnDocumentDidLoad];
    } else if (self.document.documentState == UIDocumentStateNormal) {
        OnDocumentDidLoad(YES);
    }
}

'

Using the context from the document I create NSManagedObjects (standard Core Data stuff, right?). The problem is that I get this error when I run the app:

"Can't merge models with two different entities named"

I'm in over my head with this DocumentHandler, and I hope this is an easy fix I'm too stupid/inexperienced to see.

I havent' made separate versions of the data model or anything and I'm not trying to migrate anything. I just want to run this app for the first time!

Thanks for reading.

Community
  • 1
  • 1
Caborca87
  • 187
  • 1
  • 16
  • OK, I changed the name of the .coredatamodel thing and changed it back and that fixed this issue (weird...). But now I get this: **'NSInvalidArgumentException', reason: ' -[__NSArrayM insertObject:atIndex:]: object cannot be nil'** at '[UIManagedDocument alloc]' in my custom init method – Caborca87 May 10 '12 at 14:34
  • yes, url is non-nil. I have actually reposted the question in a much more efficient way here: http://stackoverflow.com/questions/10545613/core-data-uimanageddocument-in-xcode-4-3-cant-merge-models Thanks for reading! – Caborca87 May 11 '12 at 05:43

0 Answers0