Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core data database is empty test

How can I test if the core data database is empty? I tried:

NSIndexPath *path1 = [NSIndexPath indexPathForRow:0 inSection:0];
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:path1];
if([[managedObject valueForKey:@"date"] description]!=nil){SOMEFUNCTION}else{SOMEFUNCTION}

Thanks

like image 913
Csabi Avatar asked Feb 10 '11 12:02

Csabi


2 Answers

you have to create a fetchrequest for each entity you use in core data. if the fetchrequest returns without results you don't have objects of this entity stored in your core data.

- (BOOL)coreDataHasEntriesForEntityName:(NSString *)entityName {
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
    [request setFetchLimit:1];
    NSError *error = nil;
    NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
    if (!results) {
        LogError(@"Fetch error: %@", error);
        abort();
    }
    if ([results count] == 0) {
        return NO;
    }
    return YES;
}
like image 183
Matthias Bauch Avatar answered Nov 11 '22 02:11

Matthias Bauch


not perfect I admit but it works

my code:

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
    int fufu = [sectionInfo numberOfObjects];
    if(fufu!=0){DATABASE IS NOT EMPTY}else{DATABASE IS EMPTY}

if someone know something more efective pls post it

like image 31
Csabi Avatar answered Nov 11 '22 03:11

Csabi