Search code examples
iosdatabasecore-datansmanagedobjectcontextnsfetchrequest

Check if database is empty in iOS without of calling current NSManagedObjectContext (Core Data)?


My question is similar to the following one but has differences

I use Core Data. The problem is if I use the current NSManagedObjectContext then I may get an incorrect result. For example:

  1. From the beginning the db is filled.

  2. My app deletes all the entities via the current context.

  3. I get check if db filled and get NO but in reality the db is still filled because the context should be saved to apply the changes.

And example of my solution:

- (BOOL)isDBFilled {

    //separate context to
    NSManagedObjectContext *context = [[[NSManagedObjectContext alloc] init] autorelease];
    [context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
    [context setPersistentStoreCoordinator:coordinator];

    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:... inManagedObjectContext:context];
    [request setEntity:entity];
    [request setFetchLimit:1];
    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];
    if (!results) {

        LOG(@"Fetch error: %@", error);
        abort();
    }
    if ([results count] == 0) {

        return NO;
    }
    return YES;
}

So is it normal and safe to create another context just to check if db is filled? Or Is there better ways to perform this checking except of creating a separate BOOL variable which will be changed before db changes and after them?


Solution

  • It is fine to use a separate context to check if the database is populated or not. The overhead is minimal, so I would have no objections.

    Please note that Apple does not necessarily agree. From the "Core Data Snippets":

    Neither should a view controller create a context for its own use (unless it’s a nested context).

    That being said, I do not see the need for this. If you delete all entities, save and then check should get an empty database. If you delete all entities and do not save and then check, you should get a non-empty database. You can easily - and more easily - do this with one context.

    PS: autorelease seems to indicate that you are using a very old SDK. How about upgrading?