Monday, December 11, 2023
HomeiOS DevelopmentUnderstanding Swift Concurrency’s AsyncStream – Donny Wals

Understanding Swift Concurrency’s AsyncStream – Donny Wals


In an earlier put up, I wrote about other ways which you could bridge your current asynchronous code over to Swift’s new Concurrency system that leverages async / await. The mechanisms proven there work nice for code the place your code produces a single consequence that may be modeled as a single worth.

Nonetheless in some instances this isn’t attainable as a result of your current code will present a number of values over time. That is the case for issues like obtain progress, the consumer’s present location, and different related conditions.

Typically talking, these sorts of patterns could be modeled as AsyncSequence objects which you could iterate over utilizing an asynchronous for loop. A primary instance of this may be the traces property on URL:

let url = URL(string: "https://donnywals.com")!

for strive await line in url.traces {
    // use line
}

However what’s one of the simplest ways to construct your individual async sequences? Implementing the AsyncSequence protocol and constructing your on AsyncIterator sounds tedious and error-prone. Fortunately, there’s no motive so that you can be doing any of that.

On this put up, I’ll present you how one can leverage Swift’s AsyncStream to construct customized async sequences that produce values everytime you want them to.

Producing a easy async stream

An async stream will be produced in numerous methods. The best approach to create an async stream is to make use of the AsyncStream(unfolding:) initializer. Its utilization seems a bit as follows:

let stream = AsyncStream(unfolding: {
    return Int.random(in: 0..<Int.max)
})

In fact, this instance isn’t notably helpful by itself nevertheless it does present how easy the idea of AsyncStream(unfolding:) is. We use this model of AsyncStream each time we will produce and return return values for our async stream. The shut that’s handed to unfolding is async so which means we will await asynchronous operations from inside our unfolding closure. Your unfolding closure can be known as each time you’re anticipated to start producing a price on your stream. In observe which means your closure can be known as, you carry out some work, you come a price after which your closure is named. This repeats till the for loop is cancelled, the duty that incorporates your async for loop is cancelled, or till you come nil out of your unfolding closure.

The AsyncStream(unfolding:) approach to produce a stream of values is kind of handy nevertheless it’s notably helpful in conditions the place:

  • You need to carry out async work that must be awaited to supply parts
  • You could have a have to deal with again strain when bridging an API you personal

Whenever you’re bridging an current API that’s based mostly on delegates or for APIs that leverage callbacks to speak outcomes, you most likely received’t have the ability to use AsyncStream(unfolding:). Whereas it’s the best and least error-prone approach to construct an async stream, it’s additionally the best way that I’ve discovered to be most limiting and it doesn’t usually match properly with bridging current code over to Swift Concurrency.

Extra flexibility will be discovered within the continuation based mostly API for AsyncStream.

Producing an async stream with a continuation

When an asynchronous closure doesn’t fairly suit your use case for creating your individual async stream, a continuation based mostly strategy could be a a lot better resolution for you. With a continuation you might have the power to assemble an async stream object and ship values over the async stream each time values change into out there.

We are able to do that by creating an AsyncStream utilizing the AsyncStream(construct:) initializer:

let stream2 = AsyncStream { cont in
    cont.yield(Int.random(in: 0..<Int.max))
}

The instance above creates an AsyncStream that produces a single integer worth. This worth is produced by calling yield on the continuation. Each time we’ve got a price to ship, we should always name yield on the continuation with the worth that we need to ship.

If we’re constructing an AsyncSTream that wraps a delegate based mostly API, we will maintain on to our continuation within the delegate object and name yield each time a related delegate methodology is named.

For instance, we might name continuation.yield from inside a CLLocationManagerDelegate each time a brand new consumer location is made out there to us:

class AsyncLocationStream: NSObject, CLLocationManagerDelegate {
    lazy var stream: AsyncStream<CLLocation> = {
        AsyncStream { (continuation: AsyncStream<CLLocation>.Continuation) -> Void in
            self.continuation = continuation
        }
    }()
    var continuation: AsyncStream<CLLocation>.Continuation?

    func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places: [CLLocation]) {

        for location in places {
            continuation?.yield(location)
        }
    }
}

The instance above is a really naive start line for creating an async stream of consumer places. There are a few issues we don’t totally take note of equivalent to cancelling and beginning location remark or asking for location permissions.

At its core although, this instance is a superb start line for experimenting with async streams.

Observe that this strategy is not going to look ahead to shoppers of your async stream to devour a price totally earlier than you possibly can ship your subsequent worth down the stream. As an alternative, all values that you just ship can be buffered in your async stream by default which can or will not be what you need.

In sensible phrases which means while you ship values down your stream sooner than the consuming for loop can course of these values, you’ll find yourself with a buffer full of values that can be delivered to the consuming for loop with a delay. This could be precisely what you want, but when the values you ship are considerably time delicate and ephemeral it could doubtlessly make sense to drop values if the consuming for loop isn’t able to obtain values.

We might determine that we by no means need to maintain on to greater than 1 location and that we solely need to buffer the final identified location to keep away from processing stale knowledge. We are able to do that by setting a buffering coverage on our async stream:

lazy var stream: AsyncStream<CLLocation> = {
    AsyncStream(bufferingPolicy: .bufferingNewest(1)) { (continuation: AsyncStream<CLLocation>.Continuation) -> Void in
        self.continuation = continuation
    }
}()

This code passes a bufferingPolicy of .bufferingNewest(1) to our AsyncStream. Which means that we are going to solely buffer a single worth if the consuming for loop isn’t processing objects quick sufficient, and we are going to discard older values in favor of protecting solely the newest location.

If our stream involves a pure shut, you possibly can name end() in your continuation to finish the stream of values.

In case your stream would possibly fail with an error, you may as well select to create an AsyncThrowingStream as an alternative of an AsyncStream. The important thing distinction is that customers of a throwing stream should await new values utilizing strive await as an alternative simply await. To make your stream throw an error you possibly can both name end(throwing:) in your continuation or you possibly can name yield(with:) utilizing a End result object that represents a failure.

Whereas the fundamentals of constructing an AsyncStream aren’t notably advanced, we do want to consider how we handle the lifecycles of the issues we create fastidiously. Particularly as a result of we’re not speculated to make our continuations outlive our streams which is an easy mistake to make while you’re bridging current delegate based mostly code.

Managing your stream’s lifecycle

There are primarily two methods for an async stream to finish. First, the stream would possibly naturally finish producing values as a result of no additional values will be produced. You’ll name end in your continuation and you’ll present any cleanup that that you must do on the identical time. For instance, you possibly can set the continuation that you just’re holding on to to nil to ensure you can’t by accident use it anymore.

Alternatively, your stream can finish as a result of the duty that’s used to run your async stream is cancelled. Contemplate the next:

let places = AsyncLocationStream()

let process = Process {
    for await location in places.stream {
        print(location)
    }
}

process.cancel()

When one thing just like the above occurs, we are going to need to make it possible for we don’t name yield on our continuation anymore except we begin a brand new stream with a brand new, energetic, continuation.

We are able to detect and reply to the tip of our stream by setting an onTermination handler on our continuation:

self.continuation?.onTermination = { end in
    print(consequence)
    self.continuation = nil
}

Ideally we set this handler instantly after we first create our async stream.

Along with the stream being cancelled or in any other case going out of scope, we might break out of our loop which can finally trigger our process to complete. That is typically talking not one thing this can finish your async stream so if you need breaking out of your loop to finish your stream, you will have to take this into consideration your self.

Personally, I’ve discovered that the simplest approach to ensure you do some cleanup is to have some methodology in your stream producing object to cancel the stream as an alternative of simply breaking out of an async for loop. That method, you possibly can carry out cleanup and never have a stream that’s sending values though no person is listening.

It’s additionally vital to keep in mind that the sample I confirmed earlier will solely work if one client makes use of your location stream object. You can not have a number of for loops iterating over a single stream in Swift Concurrency as a result of by default, async sequences lack the power to share their iterations with a number of loops.

In Abstract

On this put up, you discovered so much about async streams and how one can produce your individual async sequences. First, you noticed the unfolding strategy of constructing an async stream and also you discovered that this strategy is comparatively simple however won’t be very helpful for those that have to bridge current delegate or callback based mostly APIs.

After exploring unfolding for a bit, we took a take a look at the construct closure for async streams. You discovered that this strategy leverages a continuation object that may be known as to supply values if and when wanted.

You noticed a really rudimentary instance of an object that will bridge a CLLocationManager into async await, and also you discovered a however about appropriately managing your continuations to stop sending values into an already accomplished stream.

When you’ve got any questions or feedback for me about this put up, please be happy to succeed in out on Twitter or on Mastodon.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments