如何正确配置NSFetchedResultsController

时间:2009-09-01 16:48:35

标签: iphone core-data

我将NSFetchedResultsController放入我的代码中,这样我就可以自动切换表格视图数据。

所以我正在运行测试以确保一切正常。我的持久性商店中有一个Book实体。我将首先执行以旧方式获取,然后我将尝试使用NSFetchedResultsController。 2个代码块之间的差异只有2行。

代码没有 NSFetchedResultsController:

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:kBookEntityName inManagedObjectContext:self.managedObjectContext];
 [request setEntity:entity];

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

  //The following 2 lines will be replaced by the NSFetchedResultsController
 NSMutableArray *mutableFetchResults = [[[self.managedObjectContext executeFetchRequest:request error:nil] mutableCopy] autorelease];
 Book *result = (Book*)[mutableFetchResults objectAtIndex:0];

 NSString* title = [result valueForKey:@"title"];
 NSString* priority = [result valueForKeyPath:@"priority.name"];


 [request release];

现在我在行中替换NSFetchedResultsController:

NSFetchedResultsController* fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"title" cacheName:@"BookList"];                                                                                                                                                                                                                                                                                                           

Book *result = (Book*)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

看起来很干爽。第一个代码块正确地获取单个Book实体。但是,具有NSFetchedResultsController的代码却没有。相反,它返回nil。

我的问题是:我是否在此示例中正确配置了NSFetchedResultsController?

(注意,NSFetchedResultsController的fetchedObjects属性也是nil)

2 个答案:

答案 0 :(得分:13)

我认为你仍然需要告诉NSFetchedResultsController实际执行fetch:

NSError *error;
BOOL success = [controller performFetch:&error];

(取自NSFetchedResultsController参考中的示例)

另外一件看起来很奇怪的事情是:你真的想把“标题”用作sectionNameKeyPath吗?这基本上不会为每本书创建一个单独的部分吗?

答案 1 :(得分:7)

您只需要初始化NSFetchedResultsController一次,如下所示。代码假设

NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;

在控制器的标头中声明,并且该managedObjectContext已经正确初始化。

- (void)viewDidLoad {

    [super viewDidLoad];

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    }

    NSLog(@"%d objects fetched", [[fetchedResultsController fetchedObjects] count]);
}

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;

    }

    /*
     Set up the fetched results controller.
     */



    // Create the fetch request for the entity.
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:kBookEntityName inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];

/* Optional settings

    [request setResultType:NSManagedObjectResultType];
    [request setIncludesPropertyValues:YES];
    [request setIncludesPendingChanges:NO];
    [request setReturnsObjectsAsFaults:NO];
*/  


    // Edit the sort key as appropriate.
     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 [request setSortDescriptors:sortDescriptors];
 [sortDescriptor release];
 [sortDescriptors release];;

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:@"title" cacheName:@"BookList"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [request release];


    return fetchedResultsController;
}