我怎么能等待本身具有异步操作完成的功能?等功能与异步操作完成
让我给你举个例子:
Function_A
获取从图库中的照片。提取操作将使用提取闭包异步执行。
我打电话Function_A
在Function_B
,我想等待照片抓取操作完成,然后继续我的代码。
我已经试过什么:
我用调度组,但并未奏效。我也尝试在同步块中调用Function_A
,这也是不成功的。
我怎么能等待这个操作完成?可以因为将读取操作运行到另一个队列而导致此问题?
更新:
这是我做过什么:
class imageFetch: NSObject{
var theImage: PHAsset? = nil
func Function_A(){
let fetchOption = PHFetchOptions()
PHPhotoLibrary.requestAuthorization { (authStatus) in
// It seems code will be executed asynchronously from here
if authStatus == .authorized{
let imageAsset = PHAsset.fetchAssets(with: .image, options: fetchOption)
// Select image and save it in theImage
// I know I can send a closure and run it here
}
}
}
}
而在另一个I类中调用该函数:
class edit: NSObject{
funct Function_B(){
var imageFetchIns = imageFetch()
// I want below code to execute asynchronously
imageFetchIns.Function_A()
// Now I want to wait last operation to finish
}
}
你有没有看着承诺库? – Malik
不确定调度组为什么不起作用。尝试用它显示代码。 – zach274
也RxSwift将解决这个任务很容易 – iWheelBuy