Combine and Function Builders

In my last post Property Wrappers with Combine I wrote about using Swift 5.1 ’s property wrappers to spice up some of my Combine code. Since mixing up the latest Swift 5.1 hotnesss with Combine felt pretty good I thought this time I’d write about function builders. So let’s get started. Swift Function Builders Function Builders are a new feature in Swift 5.1 that allows you to abstract redundant functionality around executing chunks of code that produce similar results. …

Posted on

Property Wrappers with Combine

In this post I’ll look into making more out of Combine code by using the newly introduced in Swift 5.1: Property Wrappers. Let’s very quickly go into what property wrappers are in general and then dive into a Combine example. Swift Property Wrappers Property wrappers are a new feature in Swift 5.1 that allows you to abstract redundant functionality around handling type properties. Property wrappers are very handy in (my experience so far 🤓 ) two ways: …

Posted on

subscribe(on:) vs receive(on:)

In this post, instead of looking into the basics of multi-threading with Combine, we are going to have a look at the difference between subscribe(on:) and receive(on:) specifically. We’re going to look at a typical subscription chain starting with a root publisher, a couple of operators, and a subscriber at the end. We’ll look into more diagrams and some example code of how subscribe(on:) and receive(on:) affect subscriptions. subscribe(on:) subscribe(on:) sets the scheduler on which you’d like the current subscription to be “managed” on. …

Posted on

Binding a list with multiple sections and different cells

Today’s post showcases another real-life use case of using CombineDataSources, namely binding a list to a table view with multiple sections using different cell types. In my last post Binding a simple list to a UITableView we had a look at binding a list of data elements to a table view. It’s pretty simple to bind a plain table view when you have a coherent list of elements. In this post I’m gonna show some code on how to bind a list to a table with multiple sections that also needs to use different cell types for the different sections. …

Posted on

Binding a simple list to a UITableView

While still keep improving the current version of CombineDataSources I tought it’d be nice to post few articles on the current API design along with some code samples. This would be nice in order to get some feedback and to try putting some of the code in context. CombineDataSources Introduction RxSwiftCommunity’s excellent RxDataSources allows developers to reactively bind data to table and collection views. It uses a lot of the underlaying structure of RxSwift and it’s very powerful. …

Posted on

An assert operator: assertMaxSubscriptions()

While I keep working on CombineDataSources I’ve faced some of the differences between RxSwift and Combine and those are slowing me down a bit. For example: in RxSwift when dealing with UI I’d usually use shareReplayLatestWhileConnected() - this operator shares an observable and emits the latest value to all subscribers downstream. In Combine there is currently a share() operator which does a similar thing but does not “replay” the latest value. …

Posted on

Debug logging with CombinePrintout

Logging is a really powerful way to debug code, especially if it’s asynchronous code that runs in a time sensitive way so stepping through the debugger is a pain. In Simple custom Combine operators I wrote about how to create an operator that logs to the system Console app in just few lines. After playing with Combine some more I went ahead and created a micro framework called “CombinePrintout” that adds few convenient methods to Combine allowing you to quickly print information about your subscriptions. …

Posted on

Building a custom `sample` operator

In Simple custom Combine operators I spoke about how you can cheaply create your own custom Combine operator without the need to create a new custom Publisher type. In this post I’d like to show how this kind of “cheap” operators could actually be quite complex and useful; still being created only by creatively combining existing operators. Let’s look at sample(_)… The sample operator rxmarbles.com is a marveleous website where you can find visual and interactive presentations of a multitude of reactive operators (based in their implementation in RxJS). …

Posted on

Simple custom Combine operators

Creating simple custom operators is very straightforward in Combine. As long as your custom operator can be expressed via combining existing operators you don’t need to create a new publisher type - you can simply extend the Publisher protocol with your own custom operator. Let’s try quickly creating one now. Extending Publisher You can extend Publisher like any other protocol in Swift. For this post I’ll create a debugging operator that writes logs to the system console. …

Posted on