【发布时间】:2023-03-21 21:00:01
【问题描述】:
我有一个包含 300 个单元格的集合视图,由 NSFetchedResultsController 驱动。每隔一段时间,所有的对象都会更新,所以我会收到委托消息告诉我,我让集合视图处理更新,就像处理表格视图一样。不幸的是,每次发生这种情况时它都会将主线程锁定几秒钟......我不知道为什么。这是我的代码:
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.cascadeViewController.collectionView performBatchUpdates:^{
NSLog(@"performingBatchUpdates");
for (NSDictionary *change in self.changes) {
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
if (type == NSFetchedResultsChangeInsert) {
[self.cascadeViewController.collectionView insertItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeDelete) {
[self.cascadeViewController.collectionView deleteItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeUpdate) {
[self.cascadeViewController.collectionView reloadItemsAtIndexPaths:@[obj]];
} else if (type == NSFetchedResultsChangeMove) {
[self.cascadeViewController.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
}
}];
}
NSLog(@"performingBatchUpdates end");
} completion:^(BOOL finished) {
NSLog(@"completion");
// TODO: implement
// [self configureMessageAndFooterView];
}];
NSLog(@"end of method");
[self.changes removeAllObjects];
}
这里发生了什么?在我的应用程序的实际执行中,一次更新所有 300 个对象不会经常发生,但足以让我担心它。我正在使用库存 UICollectionViewFlowLayout - 我需要做一些更自定义的事情吗?
【问题讨论】:
-
你应该分析它。
-
我有 - 它在内部 API 调用上做了很多事情。它命中 [UICollectionViewUpdate _computeGaps] 并且似乎对 NSArrays 和 indexPath 比较进行了很多排序。似乎没有调用任何我可以修改的委托方法,或者我可以替换的 UICollectionViewFlowLayout 方法。
-
您在此期间解决了问题吗?我的 collectionview 中的 600 个项目也遇到了同样的问题,将主线程锁定了 20 秒!
标签: objective-c uicollectionview nsfetchedresultscontroller uicollectionviewlayout