gpt4 book ai didi

swift - 等待具有异步操作的函数完成

转载 作者:行者123 更新时间:2023-11-28 15:21:08 24 4
gpt4 key购买 nike

<分区>

如何等待本身具有异步操作的函数完成?

让我举个例子:

Function_A 从图库中获取照片。获取操作将使用获取闭包异步执行。

我在 Function_B 中调用了 Function_A,我想等待照片获取操作完成,然后继续我的代码。

我尝试过的:

我已经使用了调度组,但它没有用。我还尝试在同步块(synchronized block)中调用 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
}
}
}
}

在另一个类中我调用该函数:

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com