gpt4 book ai didi

ios - 依次执行Dispatch main

转载 作者:行者123 更新时间:2023-11-30 12:44:15 24 4
gpt4 key购买 nike

如何按顺序执行一组调度代码。例如:

Dispatch.main.async{
//call API, it may takes 5-10 seconds..
//do something..
print("done 1")
}

Dispatch.main.async{
//call API, it may takes 5-10 seconds..
//do something
print("done 2")
}

Dispatch.main.asyncAfter{
print("completed")
}

Output:
completed
done1
done2

Expected output:
done1
done2
completed

我使用 main 是因为“做某事”会将结果填充到 UI。我尝试使用 global() 但“已完成”总是先执行,并且不适合使用调度时间延迟,因为无法确认 API 调用时间有多长。需要快速建议〜

最佳答案

 DispatchQueue.main.async{
//call API, it may takes 5-10 seconds..
//do something..
print("done 1")
}

DispatchQueue.main.async{
//call API, it may takes 5-10 seconds..
//do something
print("done 2")
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1){
print("completed")
}

It will execute in sequence

关于ios - 依次执行Dispatch main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845249/

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