Base Database - Empty Test

How to check if the kernel 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}

thank

+5
source share
3 answers

you need to create a fetchrequest for each object that you use in the master data. if fetchrequest returns without results, you have no objects of this object stored in your master 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;
}
+18
source

Not perfect I admit, but it works

my code is:

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 knows something more efficient pls post it

+1
source

, appDelegate:

- (NSString *)applicationDocumentsDirectory 

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    return basePath;

}

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 

    {

        if (persistentStoreCoordinator != nil) 

            return persistentStoreCoordinator;

        NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"YourApp.sqlite"]];

        NSLog(@"storeURL: %@", storeUrl);

        NSError *error;

        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

        NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:  

                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  

                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];  

        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 

        {

            /* 
            Replace this implementation with code to handle the error appropriately. 

            abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be 
                    useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

            Typical reasons for an error here include: 
            * The persistent store is not accessible 
            * The schema for the persistent store is incompatible with current managed object model 
            Check the error message to determine what the actual problem was. 
            */


        }// if    

        return persistentStoreCoordinator;
    }

StoreUrl sqlite.

If you open this path with the sqlite manager, you can see the contents of your sql database. I use this SQLite Manager to analyze sqlite databases: SQLite Manager

(This method can only be used on the simulator)

0
source

All Articles