为什么我必须注释掉我的NSPredicate才能让NSFetchedResultsController填充UITableView?

时间:2013-10-25 09:32:25

标签: ios uitableview concurrency nsfetchedresultscontroller nsmanagedobjectcontext

我有一个奇怪的错误:如果我取消注释NSPredicate,则生成的UITableView为空。

我的数据模型如下:         类别< - >> Feed< - >>交

我正在抓取PostPost.feedPost的{​​{1}}。 Feed具有Feed rss属性。

以下是代码:

NString

正如我之前所说,如果我取消注释 - (NSFetchedResultsController *)fetchedResultsController { // Set up the fetched results controller if needed. if (_fetchedResultsController == nil) { // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:_globalMOC]; [fetchRequest setEntity:entity]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; NSPredicate *predicate =[NSPredicate predicateWithFormat:@"feed.rss == %@", _detailItem.rss]; [fetchRequest setPredicate:predicate]; // 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:fetchRequest managedObjectContext:_globalMOC sectionNameKeyPath:nil cacheName:nil]; self.fetchedResultsController = aFetchedResultsController; self.fetchedResultsController.delegate = self; NSError *error = nil; if (![self.fetchedResultsController performFetch:&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. // NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } return _fetchedResultsController; } ,我只会看到结果。我尝试使用NSPredicateLIKE==,在=周围加上双引号和单引号...

顺便说一句,最好直接比较%@对象......

根据Apple的说法,语法可能不是问题,但那又是什么?

Feed是在共享相同PersistentStoreCoordinator的单独ManagedObjectController中创建的。我得到了所需的Post的objectID,以便将新的Feed与子MOC中的相应Post相关联(否则我会收到关于关联来自不同MOC的对象的错误)

每当孩子MOC通知其变更时,我也会在主线程中合并我的MOC。

基本上:如果我Feed我拥有的NSLog(评论为NSPredicate),我会看到每个Post都有相应的RSS Post网址符合显示的{{ 1}}(= Feed)。

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:1)

如果你的 NSFetchedResultsController 是空白的,那么你很可能确定你没有通过获取请求得到任何结果而我害怕,因为不恰当的谓词或没有匹配的记录。我想问题是由于存在通配符(不太了解) 检查NSPredicate Class ReferencePredicate Programming Guide以通过谓词获得准确的结果。

答案 1 :(得分:0)

<强>尤里卡!

问题如下:当我在NSFetchedResultsController中创建DetailView时,_detailItemnil

因此,即使在设置_detailItem之后,NSPredicate仍然专注于将我的Feed关系与nil对象进行比较。

我通过以下方式刷新NSFetchedResultsController.fetchRequest didSelectRowAtIndexPath中的MasterView来解决问题:

    Feed *feed;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        feed = [_filteredCategoryArray objectAtIndex:indexPath.row];
    } else {
        feed = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    }
    self.detailViewController.detailItem = feed;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed == %@", feed];
    [self.detailViewController.fetchedResultsController.fetchRequest setPredicate:predicate];

希望这个解决方案可以帮助其他人。

感谢您的帮助,nickAtStack!