Question

I'm working with Core Data for ages. Currently I would like to learn more about UIManagedDocument. I've done several sample projects to learn more about it. Finally, I'm good to transfer this mechanism to the real projects. In the existing project, I'm currently implementing the Core Data, so I created the Data Model(with the same name as app). I've generated a class for a entity, then I've created category that has a simple method for adding objects to the database. Finally I created the singleton for the UIManagedDocument, for now it looks like this:

+ (MDManagedDocument *)sharedDocument {
    static dispatch_once_t dispatchOncePredicate;
    __strong static MDManagedDocument *md = nil;
    dispatch_once(&dispatchOncePredicate, ^{
        md = [[MDManagedDocument alloc] init];
    });
    return md;
}

- (id)init {
    self = [super init];
    if(self) {
        [self setManagedDocument:[[UIManagedDocument alloc] initWithFileURL:[[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"ThisIsDatabase"]]];
        [[self managedDocument] setPersistentStoreOptions:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]];        
    }
    return self;
}

- (void)prepareManagedDocument {
    if (![[NSFileManager defaultManager] fileExistsAtPath:[[[self managedDocument] fileURL] path]]) {
        [[self managedDocument] saveToURL:[[self managedDocument] fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"Managed document was created.");
            } else {
                NSLog(@"Error occured while creating managed document.");
            }
        }];
    } else if([[self managedDocument] documentState] == UIDocumentStateClosed) {
        [[self managedDocument] openWithCompletionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"Managed document was opened.");
            } else {
                NSLog(@"Error occured while opening managed document.");
            }
        }];
    } else if([[self managedDocument] documentState] == UIDocumentStateNormal) {
        NSLog(@"Managed document is opened and prepared for editing and/or reading.");
    }
}

After I'm instantiating shared instance and calling prepareManagedDocument method it crashes with this message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString _cfurl]: unrecognized selector sent to instance 0xb3774b0'

Interesting thing is that it only crashes at the first run, but because of this crash it won't create a persistent storage, so it won't be able to write to the document etc. I have a project which is working with this code, and don't throw any exceptions, hence it is creating persistent store and I'm able to write to this document.

Where I'm wrong?

Thanks in advance!

Was it helpful?

Solution

Issue solved! I had a category that extends NSURL, and has only one method baseURL, after I remove this category or rename the method everything works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top