Monday, March 28, 2022

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.


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.


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.

Update (2023-06-09): Nick Lockwood:

It’s interesting that SwiftUI is now ditching Combine (which was really its last remaining use-case after Swift Concurrency shipped) - I can’t recall another case of Apple walking back a new framework so quickly and thoroughly.

Marcin Krzyzanowski:

Combine.framework 🪦2023

I’m so sorry for everyone who builds a career around the framework, it kinda sucks

David Smith:

The Autolayout engine was one of the two things that ever adopted the ObjC version, used it for many years.


3 Comments RSS · Twitter

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:

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!

Leave a Comment