Muhammad Nabeel Arif
Muhammad Nabeel Arif

Reputation: 19310

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.

I am using encrypted-core-data to protect data in my project. I am able to parse and save data to 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

When I print the obj before closing the app it prints

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

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

If I use NSSQLiteStoreType instead of EncryptedStoreType everything works fine. 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.

Upvotes: 1

Views: 248

Answers (1)

Ot&#225;vio
Ot&#225;vio

Reputation: 733

tl;dr

Don't use SQLite Keywords on your model (Properties, relationships, Entities, etc).


Usually with CoreData you are safe to use SQLite reserved keywords, but apparently is not the case when using EncryptedCoreData.

This #232 issue explains it as well.

In my project I had from as a property of one of my models, and this single property was the culprit.

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. 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.

Upvotes: 0

Related Questions