【发布时间】:2013-10-25 09:32:25
【问题描述】:
我有一个奇怪的错误:如果我取消注释我的NSPredicate,生成的UITableView 是空的。
我的数据模型如下: 类别 > Feed > 帖子
我正在获取Posts。 Post.feed 是 Post 的 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;
}
正如我之前所说,如果我取消注释 NSPredicate,我只会看到结果。我试过LIKE、==、=,在%@周围加上双引号和单引号...
顺便说一句,最好直接比较Feed对象...
根据 Apple 的说法,语法可能不是问题,但那又如何呢?
Posts 是在共享相同 PersistentStoreCoordinator 的单独 ManagedObjectController 中创建的。我获得了所需的 Feed 的 objectID,以便将新的 Post 与子 MOC 中对应的 Feed 相关联(否则我会收到关于关联来自不同 MOC 的对象的错误)。
每当子 MOC 通知它发生更改时,我也会适当地将我的 MOC 合并到主线程中。
基本上:如果我有NSLog 和Posts(注释-NSPredicate),我会看到每个Post 与相关的RSS Feed URL 适合显示的Feed(= detailItem)。
谁能帮帮我?
【问题讨论】:
-
你注意到我也写了那篇文章吗?
标签: ios uitableview concurrency nsfetchedresultscontroller nsmanagedobjectcontext