2015-09-30 13 views
10

Sto creando un codice NSPersistentStore con il codice seguente.Dati principali addPersistentStoreWithType restituisce nil, ma anche l'errore è nullo

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]); 
} 

Il valore di 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; 
    }; 
} 

A questo punto è *errornil e pc è nil troppo.

Secondo la documentazione Apple se la funzione restituisce zero dovrebbe essere un errore. Qualcuno l'ha visto prima?

Il EncryptedStoreType da https://github.com/project-imas/encrypted-core-data

L'errore si verifica solo se stiamo migrando Data Store

EDIT: codice completo di metodo:

+ (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; 
} 

Io lo chiamo 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]; 
+0

Aggiorna la tua domanda con come chiami il metodo 'makeStoreWithOptions: managedObjectModel: error:' e includi come dichiari il valore 'error' che passi. – rmaddy

+0

Ho aggiornato la domanda – ppaulojr

+0

Spero che tu abbia risolto questo problema. In caso contrario, dovremmo aggiungere i file "sqlite3.h" e "sqlite3.c" al progetto per risolvere il difetto di archiviazione persistente. –

risposta

5

Innanzitutto, non controllare l'errore per uno stato di errore. Controlla solo il ritorno della chiamata a -addPersistentStoreWithType.... L'errore può essere compilato anche in condizioni non di errore.

Il tuo codice sembra buono, quindi ho il sospetto che se hai spento il negozio crittografato e utilizzato un negozio fornito da Apple SQLite allora funzionerebbe correttamente. Il che significa che il problema riguarda il codice di terze parti.

Poiché il codice di terze parti non ti fornisce un errore o un NSPersistentStore, non funziona correttamente e devi aprire un bug contro il codice di base in modo che l'autore possa indirizzarlo.

Oppure puoi attraversare quel codice di terza parte e vedere dove non funziona e perché.

+0

Grazie! https://github.com/project-imas/encrypted-core-data/issues/197 - L'ho creato lì. – ppaulojr

Problemi correlati