Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I fetch only the top 20 objects from core data
    primarykey
    data
    text
    <p>I need to find the 20 objects most recently viewed (date-stamped) by the user. Each object has a property in the core data model called dateVisited. When the user views a particular object, the dateVisited property is assigned the current date stamp.</p> <p>So, I have a 'Recent' view that shows the 20 most recently viewed objects. I'm currently using the code below to fetch and sort the data.</p> <pre><code> [fetchRequest setEntity:[NSEntityDescription entityForName:@"object" inManagedObjectContext:self.moc]]; predicate = [NSPredicate predicateWithFormat: @"objectNumber contains[cd] %@", searchTerm]; [fetchRequest setPredicate:predicate]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateVisited" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortDescriptor release]; NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.moc sectionNameKeyPath:nil cacheName:nil]; [fetchRequest release]; </code></pre> <p>The code returns the set of 3,000 objects in order, and I display the first 20. However, it's sorting all 3,000 objects and takes time to do so. It would be far more efficient if the sort only kept track of the 20 'top' encountered objects and dropped each one along the way if that object already was not in the top 20 encountered so far.</p> <p>So my question is this: Is there a way to do a fetch/sort that only keeps track of the running top 20 objects?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload