核心数据 addPersistentStoreWithType 返回 nil,但错误也为 nil

11

我使用以下代码创建了一个 NSPersistentStore

NSPersistentStore * pc = [persistentCoordinator 
                             addPersistentStoreWithType:EncryptedStoreType
                                          configuration:nil 
                                                    URL:databaseURL
                                                options:options 
                                                  error:error];

if (*error)
{
    NSLog(@"Unable to add persistent store.");
    NSLog(@"Error: %@\n%@\n%@", *error, [*error userInfo], [*error localizedDescription]);
}

选项options的值为

{
    EncryptedStore = SQLite;
    EncryptedStoreDatabaseLocation = 
 "file:///var/mobile/Containers/Data/Application/0C27F628-3FF0-467F-8EF1-5974EBBD3620/Documents/DBEncrypted.sqlite";
    EncryptedStorePassphrase = "xxxxxxxxredactedxxxxxxx";
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSSQLitePragmasOption =     {
        synchronous = OFF;
    };
}

此时*errorpc都是nil

根据苹果的文档,如果该函数返回nil,则应该是一个错误。有人之前看到过吗?

EncryptedStoreType来自https://github.com/project-imas/encrypted-core-data

只有在迁移数据存储时才会出现该错误。

编辑:方法的完整代码:

+ (NSPersistentStoreCoordinator *)makeStoreWithOptions:(NSDictionary *)options managedObjectModel:(NSManagedObjectModel *)objModel error:(NSError *__autoreleasing *)error
{
    NSPersistentStoreCoordinator * persistentCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objModel];

    //  NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    BOOL backup = YES;
    NSURL *databaseURL;
    id dburl = [options objectForKey:EncryptedStoreDatabaseLocation];
    if(dburl != nil) {
        if ([dburl isKindOfClass:[NSString class]]){
            databaseURL = [NSURL URLWithString:[options objectForKey:EncryptedStoreDatabaseLocation]];
            backup = NO;
        }
        else if ([dburl isKindOfClass:[NSURL class]]){
            databaseURL = dburl;
            backup = NO;
        }
    }

    if (backup){
        NSString *dbNameKey = (__bridge NSString *)kCFBundleNameKey;
        NSString *dbName = NSBundle.mainBundle.infoDictionary[dbNameKey];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *applicationSupportURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        [fileManager createDirectoryAtURL:applicationSupportURL withIntermediateDirectories:NO attributes:nil error:nil];
        databaseURL = [applicationSupportURL URLByAppendingPathComponent:[dbName stringByAppendingString:@".sqlite"]];

    }

    [persistentCoordinator addPersistentStoreWithType:EncryptedStoreType configuration:nil URL:databaseURL
        options:options error:error];

    if (*error)
    {
        NSLog(@"Unable to add persistent store.");
        NSLog(@"Error: %@\n%@\n%@", *error, [*error userInfo], [*error localizedDescription]);
    }

    return persistentCoordinator;
}

我称之为 “in”

- (void) initCoreDataProperties
{
    NSError *error;

    // Creating the Managed Object Model from momd
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:TBCoreDataModelFileName withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    // Creating the Encrypted Store Persistent Coordinator
    _persistentStoreCoordinator = [EncryptedStore makeStoreWithOptions: [self persistentStoreOptions]
                                                    managedObjectModel: self.managedObjectModel
                                                                 error: &error];

更新你的问题,加入你如何调用 makeStoreWithOptions:managedObjectModel:error: 方法,并包含你如何声明传入的 error 值。 - rmaddy
我已经更新了问题。 - ppaulojr
希望您已经解决了这个问题。如果没有,我们应该将“sqlite3.h”和“sqlite3.c”文件添加到项目中以解决持久存储缺陷。 - Suresh Durishetti
1个回答

6
首先,不要为错误状态检查错误。仅检查对 -addPersistentStoreWithType...的调用的返回值。即使在非错误情况下,也可能出现错误。
您的代码看起来很好,所以我怀疑如果关闭加密存储并使用苹果提供的 SQLite 存储,则它将正常工作。这意味着问题出在第三方代码中。
由于第三方代码没有向您提供错误或 NSPersistentStore,因此它的失败非常严重,您需要针对代码库打开一个错误报告,以便作者可以解决它。
或者,您可以浏览第三方代码,查看它在哪里失败以及原因。

谢谢!https://github.com/project-imas/encrypted-core-data/issues/197 - 我在那里创建了。 - ppaulojr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接