Alex
Alex

Reputation: 1581

array of NSManagedObjectID from NSFetchedResultController

I have a data set displayed in a UICollectionviewController, which I access using [fetchedResultsController objectAtIndexPath:indexPath]

Using a segue, I move onto a modal scrollview+uipageController, where each data is displayed one after the other. The problem is to access each of them individually, as the view controller does not hold the fetch request. I'm thinking passing an array of object IDs, so that each page can fetch the associated data, from the data store.

my question is: given the fetchresultController, in the UICollectionviewController I'm seguing from, is there a way to quickly create the array of Object IDs, or do I have to loop through each managedObject, obtain their ManagedObjectID, and fill it into the array ?

Upvotes: 0

Views: 424

Answers (1)

Martin R
Martin R

Reputation: 539765

You can use valueForKey to get all object IDs from an array of managed objects:

NSArray *objects = [fetchedResultsController fetchedObjects];
NSArray *objectIDs = [objects valueForKey:@"objectID"];

(Generally, sending valueForKey: to an array of objects applies valueForKey: to each object of the array, and returns an array of the results.)

Remark: When working with object IDs, keep in mind that these can change, see e.g. the objectID documentation:

Important If the receiver has not yet been saved, the object ID is a temporary value that will change when the object is saved.

Upvotes: 2

Related Questions