Questions tagged [asyncsequence]

AsyncSequence is a Swift concurrency type that provides asynchronous, sequential, iterated access to its elements.

AsyncSequence is a Swift concurrency type that provides asynchronous, sequential, iterated access to its elements.

An AsyncSequence resembles the Sequence type — offering a list of values you can step through one at a time — and adds asynchronicity. An AsyncSequence may have all, some, or none of its values available when you first use it. Instead, you use await to receive values as they become available. As with Sequence, you typically iterate through an AsyncSequence with a for await-in loop. However, because the caller must potentially wait for values, you use the await keyword.

Note, while Apple provides a variety of built-in AsyncSequence implementations (e.g. bytes(for:delegate:) in URLSession), developers can either write their own AsyncSequence or, more often, create a AsyncStream object to wrap a legacy process that emits values over time.

See also

3 questions
2
votes
2 answers

Implementing a custom asynchronous sequence in Swift

Imagine I want to create a function that, given an array of numbers, computes the square, cube, and fourth power of each number in an asynchronous fashion and returns a flattened, asynchronous sequence of all these results. So, for example, for the…
goofy4224
  • 141
  • 1
  • 8
2
votes
2 answers

TaskGroup limit amount of memory usage for lots of tasks

I'm trying to build a chunked file uploading mechanism using modern Swift Concurrency. There is a streamed file reader which I'm using to read files chunk by chunk of 1mb size. It has two closures nextChunk: (DataChunk) -> Void and completion: () -…
0
votes
1 answer

How to cancel a long term Task in ViewModel?

This is simple test but not simple. HomeView show TestView as a sheet, TestView will hide almost actions (no .task, onReceive...) into TestViewModel, TestViewModel will detect device orientation and show it on TestView. when dismiss TestView, stop…
foolbear
  • 726
  • 1
  • 7
  • 19