Sitemap

Observable 101

3 min readApr 3, 2024

--

Introduction

Hi, let’s briefly explain what is the basis of “Reactive Programming” and where we have used it before.

For Whom

New learner for “Reactive Programming”, especially RxSwift, Combine or SwiftUI frameworks. There is only the basis here, you should continue with other articles for the rest.

Purpose

Let’s start with what we want to do. There are many examples where we want to keep observe of some changes and get some work done if that change occurs.

  • To follow the values coming from the network.
  • Catching the user’s movements in the UI.
  • Monitoring the device’s status (horizontal use, dark mode, etc.)

These are generally changes that occur after a while. Of course, any changes can be watch with reactive programming, but the real need arises in pending operations.

To elaborate, constantly checking whether the user has touched a button and preparing them one by one for every other possible element is both tedious and terrible in terms of resource usage. (It’s funny for now, but when I was new learner for coding, I couldn’t think of any other method.😇)

In such cases, it is more sense to trigger the observer where the change occurs, rather than constantly checking the change. Also normalizes resource usage.

In fact, we have used reactive programming many times before with methods written in @IBAction.

Get Emin Teyhan USLU’s stories in your inbox

Join Medium for free to get updates from this writer.

By the way, “Completion Handler”, which we see a lot on iOS, is another solution to this issue, but this is the subject of another article…

So how can we do this in code?

Explanation

The simplest part of this job is coding. It is necessary to keep the changes that will occur in a variable and trigger some codes from the setter of this variable.

Instead of creating a separate method for the setter in Swift, using the property observer structure will make our job easier. You can click for “property observer” details.

class Observable<T> {
var value: T? {
didSet {
callBack?(value)
}
}
private var callBack: ((T?) -> Void)?

func bind(callBack: @escaping ((T?) -> Void)) {
self.callBack = callBack
}
}

We will store the data to be observed in a variable named “value”

We will store the data to be observed in the variable named “value”, and the operations to be triggered in the variable named “callBack”. Let’s prepare it simply.

var observableText: Observable<String> = Observable()

observableText.bind { value in
print("doing something with => \(value ?? "nil")")
}

We haven’t given any value yet, so there is no triggering. But we have prepared the variable whose value change we will observe and the code block that will be triggered.

Press enter or click to view image in full size

Conclusion

The reason why I never understood reactive programming before was that I tried to learn the codes of the frameworks directly, without understanding the 7–8 lines of code above, without knowing how it worked.

The article was very short and very basic, I know. But I want to tell everyone about these codes that I wish I knew before.

I hope it was helpful, but this is just the beginning. If we learned how it works, it’s time to learn Frameworks and perform the job.
github link for codes.

--

--