當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Swift ThrowingTaskGroup.Iterator用法及代碼示例

結構

ThrowingTaskGroup.Iterator

一種為添加到組的任務的結果提供迭代接口的類型。

聲明

struct Iterator
ChildTaskResult 符合 Sendable 並且 Failure 符合 Error 時可用。

概述

此迭代器返回的元素按任務 completed 的順序出現,而不是按這些任務添加到任務組的順序出現。

此迭代器在所有任務成功完成後終止,或者在任何任務完成後拋出錯誤。如果任務通過拋出錯誤完成,它不會返回任何進一步的任務結果。在迭代每個任務的結果之後,為任務組創建一個新的迭代器是有效的,您可以使用它來迭代您添加到組中的新任務的結果。您還可以創建一個新的迭代器以在子任務出錯後恢複迭代。例如:


group.addTask { 1 }
group.addTask { throw SomeError }
group.addTask { 2 }


do { 
    // Assuming the child tasks complete in order, this prints "1"
    // and then throws an error.
    for try await r in group { print(r) }
} catch {
    // Resolve the error.
}


// Assuming the child tasks complete in order, this prints "2".
for try await r in group { print(r) }

可用版本

iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+

相關用法


注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 ThrowingTaskGroup.Iterator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。