2014-12-05 52 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