MR_findFirstWithPredicate在非法的上下文中

时间:2013-09-20 08:11:46

标签: core-data ios7 magicalrecord magicalrecord-2.2

在以下代码中:

- (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,但它仍然是非法的。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你为什么要这样做?看起来Person对象具有Image关系。只需使用:

imageView.image = person.image ?: [UIImage imageNamed:@"default"];

无需进行更多提取操作,只会使代码难以阅读并降低应用程序速度。