2010-10-10 45 views
1

我正在使用核心數據,並且我定義了一個名爲LogRecord的實體。在其他地方,這是填充了使用對象:核心數據中的空FetchedResultsController

 LogRecord *rec = [NSEntityDescription insertNewObjectForEntityForName:@"LogRecord" 
                 inManagedObjectContext:managedObjectContext]; 
     [rec timestampNow]; 

     rec.moodType = [NSNumber numberWithUnsignedInteger:i]; 
     rec.moodValue = value; 

     NSError *error = nil; 
     if (![rec.managedObjectContext save:&error]) { 
      [DataUtil displayFatalError:error message:@"Failed to save log entry"]; 
      errors++; 
     }   

我檢查,並使用此代碼後,底層SQLite數據庫中有記錄。

然而,當我嘗試查詢使用此數據:

- (NSFetchedResultsController *)fetchedResultsController { 
    if (fetchedResultsController == nil) { 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogRecord" 
               inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // nil for section name key path means "no sections". 
    NSFetchedResultsController *aFetchedResultsController = 
     [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
              managedObjectContext:managedObjectContext 
               sectionNameKeyPath:nil 
                 cacheName:nil]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 

    [aFetchedResultsController release]; 
    [fetchRequest release]; 
    [sortDescriptor release]; 
     [sortDescriptors release]; 
    } 

    return fetchedResultsController; 
}  

通過[[self.fetchedResultsController sections] count]返回零證明它配備了空。

任何想法我可能會調試呢?這是工作之前,我試圖將此代碼分解成不同的UIViewController ....

回答

7

沒關係....不知怎的,在viewDidLoad [[self fetchedResultsController] performFetch:&error]的電話被刪除。