2012-07-20 44 views
8

我正在將我的應用程序的核心數據移至Magical Record。要更新一個UITableView,我以前有這樣的代碼:替代fetchedResultsController代碼使用magicalrecord?

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:[CoreDataStore mainStore].context]; 
    [fetchRequest setEntity:entity]; 

    [fetchRequest setFetchBatchSize:20]; 

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

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Use the sectionIdentifier property to group into sections. 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[CoreDataStore mainStore].context sectionNameKeyPath:@"sectionIdentifier" cacheName:@"Root"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 

    self.fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 
} 

我應該記住這碼的地方,還是應該使用神奇的記錄工作方式不同?

回答

14

你可以使用這樣的:

- (NSFetchedResultsController *)fetchedResultsController { 
    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    _fetchedResultsController = [NSManagedObject fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"date" ascending:NO delegate:self]; 

    return _fetchedResultsController; 
} 

你必須與你的子類來代替NSManagedObject

您可以看到其他選項here(頁眉底部)。

+1

我沒有得到任何結果。是否需要額外的步驟,例如performFetch? – Allen 2013-10-30 21:50:11