Answer the question
In order to leave comments, you need to log in
(Core Data) How to do random sort in NSFetchedResultsController?
My task is to get objects from the database in random order.
First, I generate random ids into an NSArray and pass them as a predicate:
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(self.id IN %@)", randomIds];
// Пример содержимого массива randomIds (каждый раз разный):
// <__NSArrayM 0xd1cc4e0>(185,51,69,25,33,135,136,97,157,112,145,132,56,15,159,70,88,6,72,82)
NSSortDescriptor *sortingDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
fetchRequest.sortDescriptors = @[sortingDescriptor];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question