Core data MR_findFirstWithPredicate在非法上下文中
在以下代码中:Core data MR_findFirstWithPredicate在非法上下文中,core-data,ios7,magicalrecord,magicalrecord-2.2,Core Data,Ios7,Magicalrecord,Magicalrecord 2.2,在以下代码中: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIde
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];
label.text = person.fullName;
UIImageView *iv = (UIImageView *)[cell viewWithTag:2];
NSPredicate *pr = [NSPredicate predicateWithFormat:@"person == %@", person];
Image *image = [Image MR_findFirstWithPredicate:pr inContext:person.managedObjectContext];
iv.image = image.image ? image.image : [UIImage imageNamed:@"placeholder"];
return cell;
}
这条线
Image *image = [Image MR_findFirstWithPredicate:pr inContext:person.managedObjectContext];
正在使用非法上下文。
我尝试使用本地上下文而不是person.managedObjectContext
,但它仍然是非法的
有什么想法吗?你为什么要这么做?看起来Person对象具有图像关系。只需使用:
imageView.image = person.image ?: [UIImage imageNamed:@"default"];
无需执行更多的抓取操作,这只会使代码更难阅读,也会降低应用程序的速度。Person有图像,NSSet类型。即使如此,此处指定的谓词将返回一组结果。那么为什么不直接使用[person.images anyObject]?