project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
785 stars 236 forks source link

NSFetchRequest not work after using encrypted-core-data #288

Open smartsanja2013 opened 7 years ago

smartsanja2013 commented 7 years ago

After used encrypted-core-data, all the NSFetchRequests not working. Not working means, all the NSFetchRequests returns empty array. (FYI: Before use encrypted-core-data, all things worked as expected). I am struggling to find out the issue. Please help me to find out what is the issue in my code. Expert advice is highly appreciated.

Also, I have verified I haven't use any Sqlite reserved keywords for the attributes or relationships

This is how my AppDelegate looks like

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    //NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator_Encrypted];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"XXXX" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator_Encrypted {

    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // add store
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *applicationSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
    [fileManager createDirectoryAtURL:applicationSupportURL withIntermediateDirectories:NO attributes:nil error:nil];
    NSURL *databaseURL = [applicationSupportURL URLByAppendingPathComponent:@"XXXX.sqlite"];
    NSDictionary *options = @{ EncryptedStorePassphraseKey: @"qwe123",
                               EncryptedStoreCacheSize: [NSNumber numberWithInt:1000],
                               EncryptedStoreDatabaseLocation: databaseURL
                               };

    _persistentStoreCoordinator = [EncryptedStore makeStoreWithOptions:options managedObjectModel:[self managedObjectModel]];

    return _persistentStoreCoordinator;
}

This is how the data will be save to coredata

+ (void)saveTopicSequenceInCoreData:(NSString *)strSequnce forRegionType:(NSString *)regionType withCategoryId:(NSString *)categoryId{

// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity;

entity = [NSEntityDescription entityForName:@"LocalSequence"
                         inManagedObjectContext:context];

[fetchRequest setEntity:entity];

// Fetch existing categories
NSError *error;

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(categoryName=%@)", categoryId];

NSArray *existingCategories = [context executeFetchRequest:fetchRequest error:&error];

if(existingCategories.count > 0){
    DLog(@"Category exsits");
    LocalSequence *aSeq = [existingCategories objectAtIndex:0];

    aSeq.categoryName = [NSString stringWithFormat:@"%@",categoryId];
    aSeq.strSequence = [NSString stringWithFormat:@"%@",strSequnce];
}
else{
    DLog(@"Category not exsits");
    LocalSequence *aSeq = [NSEntityDescription
                               insertNewObjectForEntityForName:@"LocalSequence"
                               inManagedObjectContext:context];;

    aSeq.categoryName = [NSString stringWithFormat:@"%@",categoryId];
    aSeq.strSequence = [NSString stringWithFormat:@"%@",strSequnce];        
}

fetchRequest = nil;

// save context
if (![context save:&error]) {
    DLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

}

At this point I have put a debug point and print the "existingCategories". I can see data have saved to entity.

<__NSArrayM 0x6000006520f0>(
<LocalSequence: 0x600000682da0> (entity: LocalSequence; id: 0x600000238cc0 <x-coredata://E1D4A435-56E8-4916-B3E3-78F84654808C/LocalSequence/p2> ; data: {
    categoryName = 10795;
    strSequence = "18185392,18182801,18149147,18148564,17849920,18139834,18123510,18115317,18091256,18108872,18111979,18107294,18070966,18073714,18071669,17785832,18054718,18010606,18013604,18012602,18011196,18003865,18";
})
)

Now, this is my fetch method

+ (NSArray *)fetchTopicSequenceFromCoreData:(NSString *)regionType withCategoryId:(NSString *)categoryId start:(int)start end:(int)end{

// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity;

entity = [NSEntityDescription entityForName:@"LocalSequence"
                         inManagedObjectContext:context];

[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];

// Fetch existing categories
NSError *error;

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(categoryName=%@)", categoryId];

NSArray *existingCategories = [context executeFetchRequest:fetchRequest error:&error];

return existingCategories;
}

This is how the fetch request looks like.

<NSFetchRequest: 0x6000002c7310> (entity: LocalSequence; predicate: (categoryName == "10795"); sortDescriptors: ((null)); type: NSDictionaryResultType; includesPendingChanges: NO; )

My problem is, after execute the fetch request, why existingCategories array always a empty array? Hope my question is detailed enough. Thanks

pratap511 commented 4 years ago

Can you please help out on this issue?

spell931 commented 3 years ago

@smartsanja2013 Have you been able to resolve this issue?

revathi-ram commented 3 years ago

Any solutions for this issue now? Kindly update.

ThePragmaticArt commented 3 years ago

Any solutions for this issue now? Kindly update.

You guys do realize this project hasn't been actively maintained in years right? 😕. Asking for solutions to your problems here is mostly tossing a coin into a wishing well. For the most part, you guys will have to debug your own issues...

revathi-ram commented 3 years ago

To anyone who is looking for this still, for Dictionary type results, the objects are fetched if we pass propertiesToFetch in the fetch request.