集合视图的reloadData与PHPhotoLibraryChangeObserver冲突

时间:2019-05-07 20:47:34

标签: ios swift uicollectionview reloaddata

我有一个UICollectionView,可以显示用户的照片库图片。我允许用户选择多张照片,经过一些处理后,我将更改后的照片作为新照片保存到照片库(不删除旧照片)。我使用这种方法保存照片:

fileprivate func saveImagesToLibrary(imageArray:[UIImage]){

    var savedImageNum = 0
    let increment = 0.8 / Float(imageArray.count)
    var percentage:Float = 0.2

    self.getAlbumWithName(name: albumNameToCreate) { (assetCollection) in

        if let collection = assetCollection {
            let group = DispatchGroup()

            for (_,image) in imageArray.enumerated(){
                group.enter()
                PHPhotoLibrary.shared().performChanges({
                    let request = PHAssetChangeRequest.creationRequestForAsset(from: image)
                    let placeholder = request.placeholderForCreatedAsset
                    let albumChangeRequest = PHAssetCollectionChangeRequest(for: collection)
                    if let placeholder = placeholder, let albumChangeRequest = albumChangeRequest {
                        albumChangeRequest.addAssets([placeholder] as NSArray)
                    }

                }, completionHandler: { (success, error) in

                    DispatchQueue.main.async {
                        percentage = percentage+increment
                        SVProgressHUD.showProgress(Float(percentage), status: "Saving photos...")
                    }

                    group.leave()
                    if success == true {
                        savedImageNum += 1
                    } else {
                        if let inError = error {
                            print(inError)

                        }
                    }
                })
            }

            group.notify(queue: .main, execute: {

                var status = ""
                if savedImageNum == 0 {

                    self.dismissProgressHUDWithError()

                } else if savedImageNum == 1 {
                    status = "Saved \(savedImageNum) image out of \(self.selectedAssets.count) in \"Saved from Xif\" album"

                    SVProgressHUD.showSuccess(withStatus: status)
                    SVProgressHUD.dismiss(withDelay: 3)
                    self.view.isUserInteractionEnabled = true
                    self.removeAllSelections()
                    self.dismissDimmerView()

                } else {
                    status = "Saved \(savedImageNum) images out of \(self.selectedAssets.count) in \"Saved from Xif\" album"

                    SVProgressHUD.showSuccess(withStatus: status)
                    SVProgressHUD.dismiss(withDelay: 3)
                    self.view.isUserInteractionEnabled = true
                    self.removeAllSelections()
                    self.dismissDimmerView()

                }
            })

        } else {

            DispatchQueue.main.async {
                self.dismissProgressHUDWithError()
            }

        }
    }

}

如上所述,我使用removeAllSelections()取消选择所有单元格:

fileprivate func removeAllSelections(){

        selectedAssets = []

        disableDeleteButton()

        photoCollectionView.reloadData()
    }

我也在代码的其他地方使用removeAllSelections(),并且一切正常,但是当我在saveImagesToLibrary方法中使用它时,有时无法删除选择。似乎reloadDeta()命令无法正常运行,因为它与PHPhotoLibraryChangeObserver调用冲突,该调用也尝试更新集合视图:

extension centerViewController: PHPhotoLibraryChangeObserver {

    func photoLibraryDidChange(_ changeInstance: PHChange) {

        guard let changes = changeInstance.changeDetails(for: inFetchResult)
            else { return }

        // Change notifications may originate from a background queue.
        // As such, re-dispatch execution to the main queue before acting
        // on the change, so you can update the UI.
        DispatchQueue.main.sync {

            // Hang on to the new fetch result.
            inFetchResult = changes.fetchResultAfterChanges
            updateSelectedAssetsAfterChange()
            // If we have incremental changes, animate them in the collection view.
            if changes.hasIncrementalChanges {
                guard let collectionView = self.photoCollectionView else { fatalError() }
                // Handle removals, insertions, and moves in a batch update.
                collectionView.performBatchUpdates({
                    if let removed = changes.removedIndexes, !removed.isEmpty {
                        collectionView.deleteItems(at: removed.map({ IndexPath(item: $0, section: 0) }))
                    }
                    if let inserted = changes.insertedIndexes, !inserted.isEmpty {
                        collectionView.insertItems(at: inserted.map({ IndexPath(item: $0, section: 0) }))
                    }
                    changes.enumerateMoves { fromIndex, toIndex in

                        collectionView.moveItem(at: IndexPath(item: fromIndex, section: 0),
                                                to: IndexPath(item: toIndex, section: 0))
                    }
                })
                // We are reloading items after the batch update since `PHFetchResultChangeDetails.changedIndexes` refers to
                // items in the *after* state and not the *before* state as expected by `performBatchUpdates(_:completion:)`.
                if let changed = changes.changedIndexes, !changed.isEmpty {

                    collectionView.reloadItems(at: changed.map({ IndexPath(item: $0, section: 0) }))


                }
            } else {
                // Reload the collection view if incremental changes are not available.
                photoCollectionView.reloadData()

            }


            resetCachedAssets()
            updateFooter()
        }
    }
}

我尝试将removeAllSelections()放在photoLibraryDidChange()中,但由于多次调用photoLibraryDidChange()而无法解决问题,因此无法找出何时完全完成所有任务。任何帮助表示赞赏。

0 个答案:

没有答案