Random entities from Core Data to-many relationship
I'm trying to setup a core data model like this: User-> Friends-> Albums -> Pictures (Minus User, each of these is a to-many relationship)
I need to have the ability to randomly select X amount of pictures; preferably, I would like to avoid having to randomly choose a few Friends, then from those a few Albums and 开发者_如何转开发finally a few pictures- my core data predicate experience is quite limited so any help would be greatly appreciated!
As far as I'm aware, there's no way to do an "order by random" in an NSFetchRequest
. Seems like a decent idea, however, so you might want to ask for it.
The closes you can get is to not specify any sort descriptors to the predicate and set a fetch limit. Without a sort descriptor, the returned objects will come in some sort of probably-non-predictable order.
Hm I'm not so sure about this myself, but couldn't you do something with the following feature:
NSSet *myFetchedPictures;
[myFetchedPictures anyObject];
Maybe in a loop (don't forget to remove the object you used from the NSSet
or you could end up with the same object again).
I'm quite new to CoreData myself, so if this is totally wrong please let me know :)
精彩评论