Question

Core data is already handling the relationship for me, do I need to build a new query to use the NSFetchedResultsController?

I have "Albums" that contain "Photos".

Album *anAlbum = [...getalbum...];

in the *cellForItemAtIndexPath: I'd like to do something with:

anAlbum.photos

However, I can't convert the indexPath to a NSSet member index. (obviously)

Without Core Data I'd typically just generate the query required myself. I'd like to make use of Core Data (again, obviously).

Was it helpful?

Solution

The "Photo" entity (anAlbum.photos is the relationship) contains the asset url. I have no issue with the displaying it was more of a concern of how do I use the NSSet (Core Data relationship) with the NSFectchedResultsController -- or direct with the view (collection/table).

First of all I would use a NSFetchedResultsController for this. This component is made to work in conjunction with tables and allows to load data in a lazy loading manner. Second, the fetch request I would use should be run against Photo entity and not against Album. In other words, you should select all the photos that belong to a specific album.

Here the code, I would use...

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
        entityForName:@"Photo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album == %@", anAlbum];
    [request setPredicate:predicate];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey:@"takeAt" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext sectionNameKeyPath:nil
            cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;

    return _fetchedResultsController;

}

So now in your tableView:cellForRowAtIndexPath: you would use

Photo *photo = [_fetchedResultsController objectAtIndexPath:indexPath];
// access the album the photo belongs to
Album* album = photo.album;

You can grasp the main concepts for NSFetchedResultsController at Core Data Tutorial for iOS: How To Use NSFetchedResultsController.

OTHER TIPS

Usually, you're going to convert the set to an NSArray (maybe with NSSet's allobjects) and perhaps sort it or filter it with a NSSortDescriptor or NSPredicate or both. Then, you can access the array by index using the index path section/row.

You can obviously use a NSMutableArray if that's what you need, but it is a pain to try to access NSSet items individually as you essentially have to enumerate them to find what you're looking for. Just create an array. Here's an example where I've done this:

NSArray *tableViewData = [NSArray arrayWithArray:[album.photos allObjects]];

Then, you just access the tableViewData objects with:

UIImage *obj = UIImageJPEGRepresentation([tableViewData objectAtIndex:indexPath.row], 0.50f);

Something like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top