当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。