Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize NSFetchedResultsController fetch
    primarykey
    data
    text
    <p>I have an NSFetchedResultsController and a large amount of data stored in Core Data which the fetched results controller must fetch. There are about 46,000 items it will end up fetching. The problem is that the fetch takes well over 8 seconds, and the entire time, the UI is frozen because we have to fetch on the main thread. I was wondering if there is something I can do to speed this up?</p> <p>I am sorting the objects by their creation date, the fetch request has a batch size of 100, and the predicate is not overly complex. It filters the objects by their UID, type, and their owner's UID. </p> <p>Since the user can search, the fetch predicate will change and so having a cache on the fetched results controller seems pointless when it simply gets deleted every time the fetch predicate changes.</p> <p>It is directly related to the number of objects in Core Data because switching to an account that has fewer objects decreases the fetch time dramatically. (6000 objects takes less than a second). Is there any way to make this more scalable? My code follows:</p> <pre><code>- (NSFetchRequest *)getFetchRequest { NSFetchRequest *fetchRequest = [NSFetchRequest requestWithEntityName:@"Photo"]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; [fetchRequest setPredicate:[self getFetchPredicates]]; [fetchRequest setFetchBatchSize:100]; return fetchRequest; } - (NSPredicate *)getFetchPredicates { NSMutableArray *predicates = [NSMutableArray arrayWithObjects: [NSPredicate predicateWithFormat:@"life_uid == %@",[[LoginManager shared] currentLifeUID]], [NSPredicate predicateWithFormat:@"(type == %@) OR (type == %@)", TLTypeImage, TLTypeVideo], [NSPredicate predicateWithFormat:@"(ANY stories.permission.owner_person_uid == %@ OR stories.@count == 0)", [[LoginManager shared] currentPersonUID]], nil]; NSArray *filteredMomentIDs = [[self searchTerms] objectForKey:TLLibraryViewControllerSearchTermsPhotoIDsKey]; if ([filteredMomentIDs count] &gt; 0 &amp;&amp; [self searchMode] == kTLLibrarySearchModeTag) { [predicates addObject:[NSPredicate predicateWithFormat:@"uid in %@", filteredIDs]]; } return [NSCompoundPredicate andPredicateWithSubpredicates:predicates]; } </code></pre>
    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.
 

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