문제

In the following code:

- (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;
}

This line

Image *image = [Image MR_findFirstWithPredicate:pr inContext:person.managedObjectContext];

is using illegal context. I tried to use local context instead of person.managedObjectContext, but it still illegal.

Any thoughts?

도움이 되었습니까?

해결책

Why are you doing this at all? It looks like a Person object has an Image relationship. Just use:

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

No need to do more fetches that will just make the code harder to read and also slow down your app.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top