NSFetchedResultsController is loading all rows even through I have batch set

后端 未结 3 1476
轻奢々
轻奢々 2021-02-06 09:40

Ok. This is the exact same question as here: Why is NSFetchedResultsController loading all rows when setting a fetch batch size?

But the solution for his doesn\'t solve

3条回答
  •  萌比男神i
    2021-02-06 10:22

    After reading the docs on the NSFetchedResultsController initializer, I think the problem is that you have a sort in the fetch request (created) that doesn't naturally sort the same as the section key path (sectionIdentifier). The specific sentence in the docs I'm looking at says:

    If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings

    I recommend modifying your fetch request to sort on sectionIdentifier first, then created. I think that'll fix your issue.

     NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
     NSSortDescriptor* sectionSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sectionIdentifier" ascending:NO];
     // It's critical the sectionSortDescriptor is first
     [fetchRequest setSortDescriptors:@[sectionSortDescriptor, updatedSortDescriptor]];
    

    Note that if either the created or sectionIdentifier properties are actually methods on your entity class, that will definitely force Core Data to load all your data before it can sort/section, because it needs to execute that method on each entity first.

提交回复
热议问题