It’s time to discover how to manipulate the data in your Combine code
In Combine, the methods that perform an operation on values coming from a publisher are called operators.
Each Combine operator returns a publisher that, received the values, manipulates them and then send them to its subscribers.
The operators can be divided in 5 macro categories, based on how they manipulate the data:
This operators typology is useful for manipulating the values in order to make them usable for the subscribers.
Collect() operator provide a convenient way to transform a stream of individual values from a publisher into an array. This happens because collect acts as a buffer for these individual values and then it will insert them in an array after the publisher completes. …
Start your journey inside Apple framework dedicated to asynchronous programming
Before going deep inside the Combine Framework, it’s better to recall the difference between synchronous and asynchronous programming, in order to outline the context in which we are going to work.
With synchronous programming, we’re referring to all that programs wrote in a single-threaded language that execute sequentially line-by-line. Because of that, we’re always sure about the current state of our variables.
With asynchronous programming, instead, we’re referring to all that programs wrote in a multi-threaded language that are running on an asynchronous event-driven UI framework like UIKit. …
If you are experienced with iOS app development, you surely found yourself in a thread error because you tried to update the UI from a closure or something like that, and searching on the net you discovered about DispatchQueue.main and threads.
Now, you will learn something more on them and about using them with Combine.
Let’s start saying what is a Scheduler.
From Apple documentation about Combine you can read that a Scheduler is “a protocol that defines when and how to execute a closure”, and that’s true, but it’s not all. …
As you’ve seen during the Combine learning path, publishers and subscribers cooperate with each other creating a powerful tool to exchange information inside your application.
Maybe the publishers provided by the framework will be enough for your work, but what about custom publishers? Is it possible to create custom ones and, if yes, how to do it?
Of course, it is, and it’s not really complex as it may seem at first. First of all, in order to create a working stream, we need three things:
Using Combine, you see mostly publishers and subscribers — but the real magic behind the communication is made possible by subscription. …
Nowadays, even simple apps rely on Internet connectivity and no matter how hard networking theory may be, doing connectivity has always been simplified via URLSession.
As the JSON format increase in popularity, Apple has started to develop a way to combine Swift code with JSON data, giving the birth to Codable, making decoding and encoding really easy.
URLSession is the preferred way to perform network data transfer tasks. It offers a modern asynchronous API with powerful configuration options and fully transparent backgrounding support. Some of the operations supported are:
SwiftUI is an alternative to classic UIKit that allows developers to build their UI cross-platform using a declarative approach.
If you have already seen Combine code, you may notice that both Combine and SwiftUI share a declarative programming approach… but that’s not what makes them so… combinable!
The key feature of SwiftUI is the concept of Source of truth.
What does it mean? …