我可以在后台线程中获取PHAssets吗?

时间:2017-06-15 09:01:28

标签: ios swift multithreading photosframework

我想在用户的照片库上做一些工作。由于库可能很大,我想在后台进行。我想知道执行资产提取是否安全(如PHAsset.fetchAssets)并在后台处理它们?

我现在只需要资产元数据。

这样的事情会安全吗:

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)
            }
        }
    }
}

是否有任何文档参考我可以了解有关Photos Framework和后台访问的内容?

1 个答案:

答案 0 :(得分:0)

是的,可能需要一些时间来获取数据,因此在后台执行此操作可能是个好主意,因为fetchAssets(with:options:)方法是同步的。

相关问题