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.