2014-12-05 50 views
13

的錯誤我遇到了SQLCipher數據庫加密和CoreData問題: 當我將持久性存儲協調程序與SQLCipher配合使用時,它總是在第一個應用程序之後出現故障一對多關係重新啓動。 因此,當我第一次啓動應用程序時,我創建了具有關係的NSManagedObjects,然後,當我保存數據庫並重新打開該應用程序時,當我嘗試訪問這些關係時崩潰。 沒有SQLCipher一切工作正常。SQLCipher和CoreData問題:CoreData無法履行

這裏是SQLCipher持久性存儲初始化代碼:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (!_persistentStoreCoordinator) { 
     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"]; 
     NSDictionary *options = @{EncryptedStorePassphraseKey: @"MyApp", 
                  EncryptedStoreDatabaseLocation: storeURL}; 
     NSError *error; 
     _persistentStoreCoordinator = [EncryptedStore makeStoreWithOptions:options managedObjectModel:[self managedObjectModel] error:&error]; 
     if (error) { 
      NSLog(@"%@", error); 
     } 
    } 

    return _persistentStoreCoordinator; 
} 

代碼,我創建NSManagedObject:

- (id)createObjectWithClassName:(NSString *)name 
{ 
    NSManagedObject *object = [[NSClassFromString(name) alloc] initWithEntity:[NSEntityDescription entityForName:name inManagedObjectContext:self.context] insertIntoManagedObjectContext:self.context]; 
    return object; 
} 
+0

創建託管對象的代碼是什麼樣的? – 2014-12-05 17:43:05

+0

@TomHarrington我使用NSManagedObject創建代碼更新了帖子 – 2014-12-05 22:45:49

+0

有用的鏈接:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdTroubleshooting.html – Bhushan 2014-12-17 06:58:21

回答

1

最後我找到自己的答案。

我調查了不同的情況,並發現,這個問題只發生在我的數據模型中。

問題是我在名爲「index」的NSManagedObject類中有一個屬性。

看起來像SQLCipher內部使用這樣的屬性,並與它發生衝突。 一旦我將它重新命名爲其他名稱,一切正常!

1

隨着SQLCipher確保你有一個全新的SQLite數據庫。嘗試用密鑰對數據庫進行編譯時,由於某種原因它已經有數據只是試圖對其進行解密。

ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'secret'; -- create a new encrypted database 
CREATE TABLE encrypted.t1(a,b); -- recreate the schema in the new database (you can inspect all objects using SELECT * FROM sqlite_master) 
INSERT INTO encrypted.t1 SELECT * FROM t1; -- copy data from the existing tables to the new tables in the encrypted database 
DETACH DATABASE encrypted; 

-

//For persistentStoreCoordinator: 
// Give modelName = @"MyApp.sqlite"; 
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator:(NSString*)modelName 
{ 
    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 


    NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    @"PushNoticationModal.sqlite" 
    NSURL *storeUrl = [NSURL fileURLWithPath:[docs stringByAppendingPathComponent:modelName]]; 
    NSError *error = nil; 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    NSError *error; 
    persistentStoreCoordinator = [EncryptedStore makeStoreWithOptions:options managedObjectModel:[self managedObjectModel] error:&error]; 
    if (error) { 
    NSLog(@"%@", error); 
    } 

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     NSLog(@"persistentStoreCoordinator Error: %@,%@",error,[error userInfo]); 
    } 



    return persistentStoreCoordinator; 
} 
+0

你的代碼不幫我。它也有一個合乎邏輯的問題:你創建了'persistentStoreCoordinator'兩次(正常和ecnrypted),這是沒有意義的... – 2014-12-20 09:02:18

+0

上次有一些錯誤,我糾正了但由於時間不足而被bot測試。 – 2014-12-21 08:09:05