Is it possible to get PHAssets in a background thread?

I want to do some work on a custom photo library. Since the library can be huge, I want to do this in the background. I am wondering if it is safe to perform asset extraction (for example PHAsset.fetchAssets) and work with them in the background?

I only need the resource metadata.

Would it be safe:

class ViewController: UIViewController {

    var cachedResult = [Any]()

    func doBackgroundCalculationsOnPhotos(completionHandler: ([Any]) -> ()) {
        DispatchQueue.global(qos: .userInitiated).async {
            let photos = PHAsset.fetchAssets(with: .image, options: nil)
            var result = [Any]()

            photos.enumerateObjects({ asset, _, _ in  
                result.append(calculateSomething(asset))
            })

            DispatchQueue.main.async {
                self.cachedResult = result
                completionHandler(result)
            }
        }
    }
}

Are there links to documentation where I could learn about the Photos Framework and background access?

+4
source share

Source: https://habr.com/ru/post/1679327/


All Articles