Passcode Device Lock, Background Job Core Data Save

I am having issue updating core data while in background mode, I am wondering if anyone has seen this issue.

I am using encrypted core data framework here https://github.com/project-imas/encrypted-core-data


I have a background batch job setup to run every five minutes, this job basically just fetch some data from internet and update a core data database.


Everything works fine as long as the device is not locked with a password, however once the device is locked with a password then the actual database update fails.


I search around and find that any device with password locked will have all their application files locked as well. At first it was both query and save that was failing with some disk io error. I found out I need to update the file attributes of the sqllite database file with

NSDictionary *updateAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:updateAttributes ofItemAtPath:filePath error:&attributeError]){
     DDLogWarn(@"failed to update file path %@ with new file protection key attributes due to error %@", filePath, attributeError);
     return false;
}


I then also create the encrypted core data with command

NSDictionary *options = @{ NSPersistentStoreFileProtectionKey:NSFileProtectionNone,                        
NSMigratePersistentStoresAutomaticallyOption:@YES,                        
NSInferMappingModelAutomaticallyOption:@YES,                        
NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"},                        
EncryptedStoreDatabaseLocation:[self sourceStoreURL],                        
EncryptedStorePassphraseKey:password };


This seems to fix the query issue but the actual save method of object context always return false with error return as nil

[rootObjectContext performBlockAndWait:^{
NSError *error = nil;
if ([rootObjectContext hasChanges] && ![rootObjectContext save:&error]) {
     DDLogError(@"failed to save to root storage context with %@, %@", error, [error userInfo]);
     abort();
}
}];


Any one knows what is going on? I am pretty sure it has something to do with device passcode locking. Because the code works fine when in background mode but not locked. I can also get it to work from locked to unlocked but still in background. I have also print out the actual store file attribute and it looks correct.


thanks


Frank Liu

Passcode Device Lock, Background Job Core Data Save
 
 
Q