Domanda

By melting something I read on this forum and some other stuff I found in Apple's documentation, I've tried to get to a functioning NSManagedObjectContext. so, I wrote the following code:

    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]];

        NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count]>0 ? [paths objectAtIndex:0] : nil);
        NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingPathComponent:@"meetSigns.sqlite"]];

        NSError *persistentError;

        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&persistentError]) {
            NSLog([persistentError description]);
        }

        NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

I must precise that, at the beginning, my app had not data model, so I added it manually and now I have a .xcdatamodeld file. I used the interface to populate the model with my entities and relationships.
Anyway, I can't find a .sqlite file, even if it is required in the snippet I posted. Maybe, as a consequence, I get the following error:

Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x74d6dd0 {reason=Failed to create file; code = 2}

How can I create this .sqlite file? can I generate it automatically from data model? I tried to use both the name of the project and the one of data model, in both cases adding .sqlite extension, but I got same error.

Beacuse of this error, I get the following, too:

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

Coud you tell me where I'm mistaking? can you tell me how to solve the problem of mixing .sqlite file?

thanks for help

È stato utile?

Soluzione

You're using NSDocumentationDirectory in your call to NSSearchPathForDirectoriesInDomains, but it should be NSDocumentDirectory.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top