Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 核心数据数据库为空测试_Objective C_Database_Xcode_Core Data - Fatal编程技术网

Objective c 核心数据数据库为空测试

Objective c 核心数据数据库为空测试,objective-c,database,xcode,core-data,Objective C,Database,Xcode,Core Data,如何测试核心数据数据库是否为空? 我试过: 谢谢我承认这并不完美,但很有效 我的代码: id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0]; int fufu = [sectionInfo numberOfObjects]; if(fufu!=0){DATABASE IS NOT EMPTY}else{DATA

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


谢谢

我承认这并不完美,但很有效

我的代码:

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
    int fufu = [sectionInfo numberOfObjects];
    if(fufu!=0){DATABASE IS NOT EMPTY}else{DATABASE IS EMPTY}
id sectionInfo=[[self.fetchedResultsController sections]objectAtIndex:0];
int fufu=[sectionInfo numberOfObjects];
如果(fufu!=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管理器分析SQLite数据库:


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

您必须为核心数据中使用的每个实体创建一个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;
}

你为什么说这不完美?这对我来说很有效,但我是否错过了它的弱点?我只是觉得这不是一个完美的方法。但我很高兴它帮助了你
- (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;
}