如何正确配置NSFetchedResultsController [英] How to properly configure NSFetchedResultsController

查看:97
本文介绍了如何正确配置NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一个NSFetchedResultsController放入我的代码,所以我得到了很好的自动分区我的表视图数据。



所以我运行一个测试,以确保一切正常正确。我在我的持久存储中有一个单一的Book实体。我将首先执行fetch旧的方式,然后我将尝试使用NSFetchedResultsController。

代码 NSFetchedResultsController:

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

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

//以下2行将被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:@titlecacheName:@BookList 

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

看起来很干燥。第一代码块适当地提取单个Book实体。但是,使用NSFetchedResultsController的代码不会。相反,它返回nil。



我的问题是:我在这个例子中正确配置NSFetchedResultsController吗?



NSFetchedResultsController也是nil)

解决方案

我想你还需要告诉 NSFetchedResultsController 实际执行fetch:

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

(取自 NSFetchedResultsController 参考)



另一件似乎奇怪的事情:你真的想使用title作为 sectionNameKeyPath ?将不会为每本书创建一个单独的部分?


I am placing an NSFetchedResultsController into my code so I get that nice automatic sectioning of my table view data.

So I am running a test to make sure everything works properly. I have a single Book entity in my persistent store. I will first perform the fetch the old way, then I will try to use NSFetchedResultsController. The difference between the 2 blocks of code is just 2 lines.

Code without 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];

Now I substitute in the lines for the NSFetchedResultsController:

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

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

Seems pretty cut and dry. The first code block properly fetches the single Book entity. The code with the NSFetchedResultsController, however, does not. Instead it returns nil.

My question is: Am I properly configuring the NSFetchedResultsController in this example?

(note, the fetchedObjects property of the NSFetchedResultsController is also nil)

解决方案

I think you still need to tell the NSFetchedResultsController to actually perform the fetch:

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

(taken from the example in the NSFetchedResultsController reference)

one other thing that seems odd: do you really want to use "title" as the sectionNameKeyPath? won't that basically create a separate section for each book?

这篇关于如何正确配置NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆