简体   繁体   中英

Core Data - persistent store can not be created on iOS 5.1

I get an error when I want to create a persistent store. When I build for iOS 5.0 there is no error and core data works successfully.

Here's the code for create the persistent store:

+ (NSManagedObjectContext *)getContext {

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES],
                             NSInferMappingModelAutomaticallyOption, nil];


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"Database.sqlite"]];
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
    NSError *error = nil;

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        NSLog(@"error loading persistent store..");
        [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //abort();
        }
    }


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

    return context;


}

The error log:

error loading persistent store..
2012-07-15 13:16:31.440 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xde88ca0 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.455 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.458 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb5360 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.467 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.470 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb6680 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.483 Partyfinder[8213:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'
*** First throw call stack:
(0x358fa88f 0x37ca1259 0x36230fe7 0x36299287 0x593e5 0x45917 0x5bfc3 0x5c487 0x5bfc3 0x5bf6b 0x45285 0x5bfc3 0x5a883 0x5e26b 0x358ca433 0x3588f533 0x3588f77b 0x358cead3 0x358ce29f 0x358cd045 0x358504a5 0x3585036d 0x374ec439 0x3335ccd5 0x42119 0x420c4)
terminate called throwing an exception

Anyone knows how to resolve the error?

I think there are two possible scenarios for this error:

1) You created that file by mistake at a previous execution of your app. This will be solved if you reset the simulator using the menu: iOS Simulator > Reset Contents and Settings, and uninstalling the app from your device: Long press > click on the x symbol.

2) There is some code somewhere else in your app that creates this file. If this is the case, you should find this code and remove it.

OR

You can go through with this link

Core Data Tips for iPhone Devs Part 2: Better Error Messages

Thanks

“解决方案”是.sqlite文件必须与项目名称具有相同的名称!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM