views:

9

answers:

1

So I want to fetch a varying number of entities that match randomly generated index numbers that are associated to the entity.

So i get a count of entities in a given set and i generate a random collection of nsnumbers within that range. now i need to pull out the entities whose index match those generated numbers ideally in a single fetch request. I think I need to write a pretty slick predicate but I'm confused as to how to account for the varying number of random selections. Sometimes I need six entities pulled other times I may need 30. How do I formulate a predicate with a varying requirement like this?

Thanks,

Nick

A: 

Do your NSManagedObject entities have an actual integer index in them? If so you can create an array of those indexes and then create a predicate like:

[NSPredicate predicateWithFormat:@"self.index in %@", arrayOfIndexes];

If your entities do not have an integer index then you will need to pull them all back into an array (because sets do not have an index) and then pull out the ones you want from that array.

Marcus S. Zarra