質問

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