简体   繁体   English

无法使用EncryptedCoreData检索以前的数据

[英]Unable to retrieve previous data with EncryptedCoreData

After app is restarted data from core-data is not returned properly. 重新启动应用程序后,无法正确返回核心数据中的数据。 And fetch request to get a particular record also fails. 并且获取特定记录的获取请求也失败。 FetchRequestController do not grabs any record once app is re-started. 重新启动应用程序后,FetchRequestController不会获取任何记录。

I am using encrypted-core-data to protect data in my project. 我正在使用加密的核心数据来保护项目中的数据。 I am able to parse and save data to managedObjectContext. 我能够解析并将数据保存到managedObjectContext。 My code looks like this 我的代码看起来像这样

for (NSDictionary *data in categories) {
    //Use MagicRecord api to get a record
    CMCategories *Obj = [CMCategories MR_findFirstByAttribute:@"uniqueId" 
                    withValue:[data valueForKey:@"id"] 
                    inContext:managedObjectContext];
    if (!Obj) {
        Obj = [CMCategories MR_createEntityInContext:managedObjectContext];
    }
    Obj.name = [data valueForKey:@"CategoryName"];
    Obj.language = [data valueForKey:@"LanguageCode"];
    Obj.uniqueId = [data valueForKey:@"id"];

}
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
    NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
    abort();
}

The code works without error. 该代码可以正常工作。 My persistentStoreCoordinator and managedObjectContext code is same as this 我的persistentStoreCoordinatormanagedObjectContext代码与相同

When I print the obj before closing the app it prints 当我在关闭应用程序之前打印obj时,它会打印

2016-01-24 23:59:11.806 Chare Dev[10556:158617] <CMCategories: 0x7feb00d24a90> (entity: CMCategories; id: 0x7feb02ef5890 <x-coredata://B947ACD3-E248-4D4F-B81E-236E100BB34D/CMCategories/p5> ; data: {
    channels =     (
    );
    language = en;
    name = Professional;
    order = 0;
    uniqueId = 15;
})

But after app restart, when we fetch all objects and print it prints like this 但是在应用重启后,当我们获取所有对象并打印时,打印结果如下:

2016-01-24 23:59:11.795 Chare Dev[10556:158617] <CMCategories: 0x7feb02de4aa0> (entity: CMCategories; id: 0x7feb02de5c10 <x-coredata://B947ACD3-E248-4D4F-B81E-236E100BB34D/CMCategories/p2> ; data: {
    channels = "<relationship fault: 0x7feb02922450 'channels'>";
    language = nil;
    name = nil;
    order = nil;
    uniqueId = nil;
})

And if I try to get a property NSString *string = obj.uniqueId 如果我尝试获取属性NSString *string = obj.uniqueId

2016-01-24 23:59:11.795 Chare Dev[10556:158617] CoreData: warning: An NSManagedObjectContext delegate overrode fault handling behavior to silently delete the object with ID '0x7feb02de5c10 ' and substitute nil/0 for all property values instead of throwing 2016-01-24 23:59:11.795 Chare Dev [10556:158617] CoreData:警告:NSManagedObjectContext委托覆盖故障处理行为,以静默方式删除ID为'0x7feb02de5c10'的对象,并将nil / 0替换为所有属性值,而不是抛出

If I use NSSQLiteStoreType instead of EncryptedStoreType everything works fine. 如果我使用NSSQLiteStoreType而不是EncryptedStoreType一切正常。 Can you guide me what am I doing wrong? 你能指导我我做错了什么吗?

Because due to above issue, database records are added multiple times and fetch request fails to fetch a managed object with uniqueId. 由于上述问题,数据库记录被多次添加,并且获取请求无法获取具有uniqueId的托管对象。

tl;dr TL;博士

Don't use SQLite Keywords on your model (Properties, relationships, Entities, etc). 不要在模型上使用SQLite关键字 (属性,关系,实体等)。


Usually with CoreData you are safe to use SQLite reserved keywords, but apparently is not the case when using EncryptedCoreData . 通常,对于CoreData来说,使用SQLite保留关键字是安全的,但是使用EncryptedCoreData显然不是这样。

This #232 issue explains it as well. #232问题也对此进行了说明。

In my project I had from as a property of one of my models, and this single property was the culprit. 在我的项目,我必须from我的车型之一的属性,而这个单一的属性是罪魁祸首。

I can see from that code: 我可以从该代码中看到:

2016-01-24 23:59:11.806 Chare Dev[10556:158617] <CMCategories: 0x7feb00d24a90> (entity: CMCategories; id: 0x7feb02ef5890 <x-coredata://B947ACD3-E248-4D4F-B81E-236E100BB34D/CMCategories/p5> ; data: {
    channels =     (
    );
    language = en;
    name = Professional;
    order = 0;
    uniqueId = 15;
})

You are using order , that is as well a keyword. 您正在使用order ,这也是一个关键字。 Try to remove and check. 尝试删除并检查。

Note: Even after renaming that property from model I had to uninstall and install the app again, not much of a hassle for me since it is not published yet. 注意:即使在从模型重命名该属性后,我也必须再次卸载并安装该应用程序,对我来说也没有太多麻烦,因为它尚未发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM