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

ppa*_*ojr 11 core-data objective-c ios

我正在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]);
}
Run Code Online (Sandbox Code Playgroud)

价值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;
    };
}
Run Code Online (Sandbox Code Playgroud)

在这一点上*errornilpcnil太.

根据Apple的文档,如果函数返回nil应该是一个错误.有没有人看过它?

EncryptedStoreTypehttps://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;
}
Run Code Online (Sandbox Code Playgroud)

我打电话给它

- (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];
Run Code Online (Sandbox Code Playgroud)

Mar*_*rra 6

首先,不要检查错误状态.只检查通话的返回-addPersistentStoreWithType....即使在非错误情况下也可以填充错误.

你的代码看起来很好,所以我怀疑如果你关闭加密的商店并使用Apple提供的SQLite商店,那么它会正常工作.这意味着问题在于第三方代码.

由于第三方代码没有向您提供错误,或者NSPersistentStore它失败了,您需要打开代码库的错误,以便作者可以解决它.

或者您可以浏览第三部分代码,看看它失败的原因和原因.