2011-02-10 10 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

ない私は認めるが、それは

私のコードに機能の完璧な:誰かが知っていれば、何かもっとefectiveのplsは

+0

なぜこれが完璧ではないと言いますか?それは私のために働いたが、私はそれが欠如していないのですか? –

+0

私はちょうどそれを行うための完全な方法だとは思わない。私はそれがあなたを助けた幸せです – Csabi

0

にそれを投稿し

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

を私は私のappDelegateで実装されたこれらの2つのメソッドを持っています:

- (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のマネージャを使用します。SQLite Manager

(あなたが唯一のシミュレータ上でこのメソッドを使用することができます)

関連する問題