【问题标题】:core data database is empty test核心数据数据库为空测试
【发布时间】:2011-06-24 19:43:43
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: objective-c database xcode core-data


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我承认并不完美,但它确实有效

      我的代码:

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

      如果有人知道更有效的东西,请发布它

      【讨论】:

      • 你为什么说这不完美?它对我有用,但我错过了它的弱点吗?
      • 我只是觉得这不是做到这一点的完美方式。但我很高兴它帮助了你
      【解决方案3】:

      我的 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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        相关资源
        最近更新 更多