ios – 即使通过批量设置,NSFetchedResultsController也会加载所有行

ios – 即使通过批量设置,NSFetchedResultsController也会加载所有行,第1张

概述好.这是完全相同的问题: Why is NSFetchedResultsController loading all rows when setting a fetch batch size? 但他的解决方案并不能解决我的问题. 我有一个有几千条记录的屏幕,加载速度很慢.我将批量大小设置为30(大约是屏幕上单元格的三倍),它仍然会循环并由于某种原因加载所有批次. 这是代码 - (NSFetched 好.这是完全相同的问题: Why is NSFetchedResultsController loading all rows when setting a fetch batch size?

但他的解决方案并不能解决我的问题.

我有一个有几千条记录的屏幕,加载速度很慢.我将批量大小设置为30(大约是屏幕上单元格的三倍),它仍然会循环并由于某种原因加载所有批次.

这是代码

- (NSFetchedResultsController *)guestCardFetchedResultsController{    if (guestCardFetchedResultsController != nil) {        return guestCardFetchedResultsController;    }    // SELECT * from GuestCard    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];    NSEntityDescription *entity = [NSEntityDescription entityForname:@"GuestCard" inManagedobjectContext:self.context];    [fetchRequest setEntity:entity];    // ORDER BY updated DESC    NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];    [fetchRequest setSortDescriptors:@[updatedSortDescriptor]];    fetchRequest.fetchBatchSize = 30;    Nsstring *cachename = self.isReportProblemVIEw ? @"reportProblemGuestCardsAll" : @"guestCardsAll";    [NSFetchedResultsController deleteCacheWithname:cachename];    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedobjectContext:self.context sectionnameKeyPath:@"sectionIDentifIEr" cachename:cachename];    aFetchedResultsController.delegate = self;    self.guestCardFetchedResultsController = aFetchedResultsController;    // Clean up    NSError *error = nil;    if (![[self guestCardFetchedResultsController] performFetch:&error]) {    }    return self.guestCardFetchedResultsController;}

在这种情况下,我没有做任何非常有趣的事情.这里是一些委托代码(不包括单元格创建,我只确认了屏幕上的单元格数量):

- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{    if ([self.guestCardFetchedResultsController.fetchedobjects count] == 0) {        return 1;    }    // Return the number of sections.    return [[self.guestCardFetchedResultsController sections] count];}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{    if ([self.guestCardFetchedResultsController.fetchedobjects count] == 0) {        return 1;    }    // Return the number of rows in the section.    ID <NSFetchedResultsSectionInfo> sectionInfo = [guestCardFetchedResultsController sections][section];    return [sectionInfo numberOfObjects];}- (Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section{    if ([self.guestCardFetchedResultsController.fetchedobjects count] == 0) {        return @"";    }    return [[self.guestCardFetchedResultsController sections][section] name];}
解决方法 在阅读了 docs on the NSFetchedResultsController initializer之后,我认为问题是你在获取请求(已创建)中有一个排序,它自然地与节键路径(sectionIDentifIEr)排序相同.我正在查看的文档中的特定句子说:

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

我建议首先修改你的获取请求以对sectionIDentifIEr进行排序,然后创建.我认为这将解决你的问题.

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]];

请注意,如果created或sectionIDentifIEr属性实际上是实体类的方法,那么肯定会迫使Core Data在排序/分段之前加载所有数据,因为它需要首先在每个实体上执行该方法.

总结

以上是内存溢出为你收集整理的ios – 即使通过批量设置,NSFetchedResultsController也会加载所有行全部内容,希望文章能够帮你解决ios – 即使通过批量设置,NSFetchedResultsController也会加载所有行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1065264.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存