- (void)photoLibraryDidChange:(PHChange *)changeInstance {
// 检测是否有资源变化
TK_WS(weakSelf);
PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:weakSelf.assetsFetchResults];
if (collectionChanges == nil) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.assetsFetchResults = [collectionChanges fetchResultAfterChanges];
NSIndexSet *removedIndexes = [collectionChanges removedIndexes];
if ([removedIndexes count] > 0) {
NSMutableIndexSet *removsectionSets = [NSMutableIndexSet indexSet];
NSMutableArray *sectionIndexPath = [NSMutableArray array];
[self.viewModels enumerateObjectsUsingBlock:^(NSMutableArray * section, NSUInteger idx, BOOL * _Nonnull stop) {
NSInteger __block count = 0;
[section enumerateObjectsUsingBlock:^(YYBCollectionCell *vModel, NSUInteger jdx, BOOL * _Nonnull stop) {
YYBPhoto *photo = vModel.model;
if (photo.isSelect) {
count = count +1;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:jdx inSection:idx];
[sectionIndexPath addObject:indexpath];
}
}];
if (count == section.count) {
[removsectionSets addIndex:idx];
}
}];
[self.photodatasManager fetchPhotoAssets:self.assetsFetchResults
completion:^(NSArray *data) {
self.dataSource = data;
self.viewModels = nil;
self.sectionViewModels = nil;
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteSections:removsectionSets];
[self.collectionView deleteItemsAtIndexPaths:sectionIndexPath];
} completion:NULL];
}];
}else{
[self loadPhotoData];
}
});
}
参考文章