核心数据库是空测试

时间:2011-02-10 12:11:52

标签: objective-c database xcode core-data

如何测试核心数据数据库是否为空? 我试过了:

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

由于

3 个答案:

答案 0 :(得分:18)

您必须为核心数据中使用的每个实体创建一个fetchrequest。如果fetchrequest返回没有结果,则表示您没有将此实体的对象存储在核心数据中。

- (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;
}

答案 1 :(得分:1)

不完美我承认但是它有效

我的代码:

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

如果有人知道更有效的内容,请发布

答案 2 :(得分:0)

我在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数据库的路径。

如果使用sqlite管理器打开此路径,则可以查看sql数据库的内容。我使用这个SQLite Manager来分析sqlite数据库:SQLite Manager

(您只能在模拟器上使用此方法)