Monday, March 28, 2022 [Tweets] [Favorites]

Swift “Async Algorithms” Package

Tony Parker (tweet):

AsyncAlgorithms is a package for algorithms that work with values over time. That includes those primarily about time, like debounce and throttle, but also algorithms about order like combineLatest and merge. Operations that work with multiple inputs (like zip does on Sequence) can be surprisingly complex to implement, with subtle behaviors and many edge cases to consider. A shared package can get these details correct, with extensive testing and documentation, for the benefit of all Swift apps.

[…]

We believe an open source package will provide a great home for these APIs. A package gives developers flexibility in deploying across both platforms and OS versions. Development and API design will take place on GitHub and the Swift Forums.

[…]

Combine’s API is based on the Publisher and Subscriber interfaces, with operators to connect between them. Its design focuses on providing a way to declaratively specify a chain of these operators, transforming data as it moves from one end to the other. This requires thinking differently about intermediate state. Sometimes this leads to call sites that are more complex than one might expect – especially when working with single values, errors, or data that needs to be shared. async/await’s Structured Concurrency provides us with a new way to express this kind of logic. We can now write asynchronous code that is split into smaller pieces and reads from top-to-bottom instead of as a series of chained transforms.

robb:

This reads very much like a eulogy to Combine

Casey Liss:

I’ve heard from birdies inside that Combine isn’t going away until SwiftUI divorces itself from Combine. But nobody internal nor external should expect any new development on it.

See also Philippe Hausler.

Previously:

Update (2022-04-13): Donny Wals:

While the basis of what we can do with both AsyncSequence and Publisher sounds similar, I would like to explore some of the differences between the two mechanisms in a series of two posts.

[…]

The post you’re reading now will focus on comparing use cases. If you want to learn more about lifecycle management, take a look at this post.

3 Comments

Combine Lover

While this sounded cool, I couldn't get it.
How do you replace Publishers and Subjects with Sequences...?

Wes Campaigne

The existing AsyncSequence protocol lets you make a task containing “for await x in asyncseuqence {“ and process each value as it is emitted asynchronously by the sequence.

You can make your own custom types conforming to this but for basic uses the new AsyncChannel looks good: https://github.com/apple/swift-async-algorithms/blob/main/Guides/Channel.md

Basically you have code in one place to .send() items into the channel and code in another place that’s sitting in a “for await” loop receiving them.

Combine Lover

Ah, that makes sense now, thank you!

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment