persistentStoreCoordinator有什么问题?

时间:2014-12-03 23:59:26

标签: ios core-data persistent-storage

任何人都可以告诉我下面的代码有什么问题吗? 它总是在包含“addPersistentStoreWithType”的行崩溃。

欢迎任何建议。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store
     _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Core_Data.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:[storeURL path]]) {
        NSURL *defaultStoreURL=[[NSBundle mainBundle] URLForResource:@"Core_Data" withExtension:@"sqlite"];

        if (defaultStoreURL) {
            // By default we use what is in the bundle read-only.
            storeURL=defaultStoreURL;
            _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
            if (![fileManager fileExistsAtPath:[storeURL path]])
                NSLog(@"RO-DB NOT FOUND");
            else NSLog(@"RO-DB FOUND");


            NSError *error = nil;
            if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                           configuration:nil
                                                                     URL:storeURL
                                                                 options:@{NSSQLitePragmasOption:@{ @"journal_mode":@"DELETE" },
                                                                           NSReadOnlyPersistentStoreOption:[NSNumber numberWithBool:YES]}
                                                                   error:&error]) {
                NSLog(@"RO-DB NOT OPENED");
            }

            return _persistentStoreCoordinator;
       }
    }

    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

其余代码只不过是Xcode在使用带有Core Data的单一视图模板构建新项目时生成的内容。

2 个答案:

答案 0 :(得分:1)

您无法添加捆绑包中的读写存储。捆绑包是只读的。

相反,首先将sqlite文件复制到文档目录,然后使用该URL将商店添加到商店协调员。

我不认为journal_mode"删除"和NSReadOnlyPersistentStoreOption是兼容的。 (无论如何,似乎没有任何意义。)也许是一个journal_mode" off"或者没有journal_mode选项会更有希望。

答案 1 :(得分:0)

这是我做错的事情: 我没有将所有必要的文件复制到捆绑包中。 我试过一两个,但有三个: File.sqlite,File.sqlite-shm,File.sqlite-wal。

希望将来可以帮助其他人。

对核心数据有更深入理解的人可能希望进一步发表评论。