Image for post
Image for post

Combine framework in action — Part 2

Diving deeper into Apple’s Combine Framework with Subscribers and Subjects.

Majid Jabrayilov
Jul 3, 2019 · 3 min read

Last week we discussed some key ideas of the framework like , , and key-path . Today I’m going to continue with another concept of : and We’ll also touch on topics like and chains.

Subscribers

In the previous post, we took a brief look at . We implemented key-path binding . Now we can check another type of in the framework, . assigns the closure to the output stream, and as soon as new value appears, it runs closure and passes a new value as a parameter. Let’s take a look at the code samples.

As you can see in the example above, we use the sink operator to create a type which runs closure as soon as new values are published.

Subjects

Another interesting type of provided by framework is can behave simultaneously as a and . You can create a instance to send some value and subscribe on it. Here is a simple example.

The framework provides two types of: and . The only difference between them is value caching. publishes only values which appear after subscription, while holds the latest value, and as soon as Subscriber assigned to , it receives the most recent value. To understand the difference between them, try to replace with and run the example.

@Published

Swift 5.1 released a new language feature called . This allows you to easily wrap properties with any custom logic. More about can be read in the proposal. framework has a special , which can be used to convert any property to a .

Schedulers

provides a nice way to control delivery and work queues. Often, we want to move a value assigned to the main-thread because of the rendering process. provides the receive operator, which is used to control receiving (delivery) queue. Similarly, we have the subscribe operator, which we can use to move the value publishing process onto a specific queue. Let’s take a look at the code example:

Both receive and subscribe operators accept or as parameters to specify a queue.

Debugging

There is a known problem in using FRP frameworks, which is difficulties during the debugging of huge chains. framework provides several operators to make debugging a bit easier. You can add a call to print operator in a chain. The “” operator will print every event in the chain to your Xcode console. Another useful tool for debugging can be a operator. It causes debugger signal which you can use to inspect an event in your debugger console.

Conclusion

Today we discussed some more key concepts of framework. It’s time to convert your asynchronous logic into . Feel free to contact me to ask your questions about the framework. Thanks for reading!


Image for post
Image for post

Snowdog is a team of creative and curious people.

Website | Twitter | Facebook

Snowdog Labs

Forefront technologies and research in the landscape of…

Majid Jabrayilov

Written by

Swift developer 👨🏻‍💻 Creator of CardioBot app for⌚️and📱Follow me on Github https://github.com/mecid or Twitter http://twitter.com/mecid

Snowdog Labs

Forefront technologies and research in the landscape of commerce, web & mobile design and development.

Majid Jabrayilov

Written by

Swift developer 👨🏻‍💻 Creator of CardioBot app for⌚️and📱Follow me on Github https://github.com/mecid or Twitter http://twitter.com/mecid

Snowdog Labs

Forefront technologies and research in the landscape of commerce, web & mobile design and development.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store