我看到NSFetchRequest的setReturnsObjectsAsFaults:NO,我看过valueForKey:used,但我似乎无法使用MR_fetchAllGroupedBy。
我称之为:
self.fetchedRC = [FeedSource MR_fetchAllGroupedBy:@"category" withPredicate:predicate sortedBy:@"category" ascending:YES inContext:self.defaultContext];
配置单元格包含:
- (void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath {
FeedSource *tempSource = [self.fetchedRC objectAtIndexPath:indexPath];
for (int j=0; j<self.tempHeaders.count; j++) {
FeedSource *myFeedSource = [self.fetchedRC objectAtIndexPath:indexPath];
if ([self.tempHeaders[j] isEqualToString:tempSource.header]) {
headerInUse = YES;
break;
}
}
MR_fetchAllGoupedBy没有使用fetchRequest的选项(我看不到),所以我不能使用setReturnsObjectsAsFaults:。
在configureCell中,tempSource具有Null。据我了解,我必须触发对象来获取数据,因为我正在调用数组,对象不会被调用来获取数据而数组无法获取数据。
那么,我需要直接从self.fetchedRC中提取数据吗?
我该怎么做?
我试过了:
MLog(@"value for key: %@", [[self.fetchedRC valueForKey:@"category"]allObjects]);
它崩溃了: 此类不是键值类别的键值编码。
我也试过了:
FeedSource *myFeedSource = [self.fetchedRC objectAtIndexPath:indexPath];
NSString *myHeader = myFeedSource.header;
NSString *myNewHeader = [myFeedSource valueForKey:@"header"];
MLog(@"Header: %@",myNewHeader);
MLog(@"Header:%@",myHeader);
他们也打印出Null。
更新: 好的,我更改了代码以使用setReturnsObjectsAsFaults并仍然有错误:
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_defaultContext];
NSFetchRequest *myFetchRequest = [FeedSource MR_requestAllSortedBy:@"category"
ascending:YES
withPredicate:predicate
inContext:localContext];
[myFetchRequest setReturnsObjectsAsFaults:NO];
[myFetchRequest setFetchBatchSize:30];
NSFetchedResultsController *myFRC = [FeedSource MR_fetchController:myFetchRequest
delegate:nil
useFileCache:NO
groupedBy:@"category"
inContext:localContext];
[FeedSource MR_performFetch:myFRC];
self.fetchedRC = myFRC;
即使valueForKey返回null:
FeedSource *myFeedSource = [self.fetchedRC objectAtIndexPath:indexPath];
MLog(@"[self.fetchedRC objectAtIndexPath:indexPath] %@",[self.fetchedRC objectAtIndexPath:indexPath]);
NSString *myHeader = myFeedSource.header;
NSString *myNewHeader = [myFeedSource valueForKey:@"header"];
MLog(@"Header using valueForKey: %@",myNewHeader);
答案 0 :(得分:0)
找到一个解决方案以防其他人遇到同样的问题: 1.将NSFetchedResultsControllerDelegate添加到标头中 2.在MR_fetchController调用中添加self to delegate。 3.添加 - (void)controllerDidChangeContent :( NSFetchedResultsController *).m的控制器方法 注意:controllerDidChangeContent:方法为空,但似乎已触发委托:self工作。