congnd / FMPhotoPicker

A modern, simple and zero-dependency photo picker with an elegant and customizable image editor
MIT License
762 stars 127 forks source link

edit multiple photos simultaneously bug #23

Open ChokShen opened 5 years ago

ChokShen commented 5 years ago

When I select multiple photos, then edit them, sometimes FMLoadingView can't hide and delegate method "didFinishPickingPhotoWith" can't response after tapping done button.Then, I set a breakpoint in DispatchQueue.main.async, and I found sometimes the code didn't enter into. ps: my device is iPhone6,iOS12.2.

` private func processDetermination() { FMLoadingView.shared.show()

    var dict = [Int:UIImage]()

    DispatchQueue.global(qos: .userInitiated).async {
        let multiTask = DispatchGroup()
        for (index, element) in self.dataSource.getSelectedPhotos().enumerated() {
            multiTask.enter()
            element.requestFullSizePhoto(cropState: .edited, filterState: .edited) {
                guard let image = $0 else { return }
                dict[index] = image
                multiTask.leave()
            }
        }
        multiTask.wait()

        let result = dict.sorted(by: { $0.key < $1.key }).map { $0.value }
        DispatchQueue.main.async { 
            FMLoadingView.shared.hide() // set a breakpoint
            self.delegate?.fmPhotoPickerController(self, didFinishPickingPhotoWith: result)
        }
    }
}`

image

Finally, I found the code "guard let image = $0 else { return }" has a bug.Because sometimes image is nil after call back , it leads to that enter() and leave() don't match, and "multiTask.wait()" will not work.

congnd commented 5 years ago

Thanks for the detailed bug report. I will try to investigate the problem and release a new fix version as soon as possible.