我有一个包含 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 - 我需要做一些更自定义的事情吗?