![](/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.