2011-02-10 92 views
5

如何測試核心數據庫是否爲空? 我想:核心數據庫爲空測試

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

感謝

回答

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

並不完美,我承認,但它的工作原理

我的代碼:

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

如果有人知道的東西更efective請張貼

+0

你爲什麼說這不完美?它爲我工作,但我錯過了它的弱點? – 2012-06-24 23:11:46

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

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