I have n number of object of an entity.
I want to fetch 30, random object from the same entity.
I'm using core data, swift 3. Could anybody help me to solve this problem?
Thanks,
I have n number of object of an entity.
I want to fetch 30, random object from the same entity.
I'm using core data, swift 3. Could anybody help me to solve this problem?
Thanks,
This is going to require an extra step, because Core Data doesn't have any built-in support for a random selection. You'll need to have some unique attribute, select your own random subset of values for that attribute, and then get the managed objects with those values.
First, you need a managed object property that has unique values. Any property will do, but numeric properties will work faster. Let's say for example you have an integer property called myUniqueID
that has unique values.
NSFetchRequestResultType.dictionaryResultType
and set the fetch request's propertiesToFetch
to include only myUniqueID
. The result will be an array of dictionaries, each containing a single value of myUniqueID
.uniqueIDArray
, use a predicate of something like NSPredicate(format: "myUniqueID in %@", uniqueIDArray)