![](/img/trans.png)
[英]How to run synchronically two functions with async operations on iOS using Swift
[英]How to perform two asynchronous operations with async let for two different types? Swift
有没有一种方法可以使用两种不同类型的async let语法执行异步操作,以便这两种方法并行运行?
我也尝试过TaskGroup但无法弄清楚
func getOptionsAndVariants() async {
do {
async let options: [ProductOption] = NetworkManager.sharedInstance.getProductOptions(product.id.description)
async let variants: [ProductVariant] = NetworkManager.sharedInstance.getProductVariants(product.id)
await try options, variants // Wrong syntax
改变
await try options, variants // Wrong syntax
到
let result = (await options, await variants)
或者,如果您正在等待的任何事情也需要try
,请将其放在相应的await
之前。 例如,如果他们都需要try
,你会说
let result = (try await options, try await variants)
现在 result 是表示返回值的两个值的元组——result.0 是options
值, result.0
是variants
值result.1
重要的是result
在两个async let
值都返回之前没有任何价值——并行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.