Swift Combine Basic 2 Minutes Tutorial, how to become the iOS developer that market needs

KD Knowledge Diet
3 min readAug 4, 2022

--

If you look at the skills companies need when hiring ios developers, combine or rxswift is a must. It looks very complicated, but if you know the basics, it doesn’t matter. Once you understand this concept, you can quickly apply and use rxjava, rxjavascript, and rxdart. It’s an incredibly useful skill, and as a developer, you’ll have to go through it someday.

Key Point

  • Combine or RxSwift consists of publisher and subscriber.
  • Handy Operator Functions.
  • All data is in the form of a stream.

Publishers

In Swift, anything can be a publisher. This means that anything can produce a data stream.

Swift Publishers

Even a simple integer value can create a data stream. You can manipulate them by subscribing to it.

Combine Specific Publishers

Combine Provided Publishers

The most confusing part of combine would be these types of publishers. You can think these are just Special Publishers from Combine. They are just Publishers. No Panic!

Subscriber

The reason combine is hard to understand is probably because there is no clear distinction between publisher and subscriber (especially for a beginner ). In some cases, the publisher becomes a subscriber. This subtle syntax makes combine hard to understand ! I know it. Because I was there.

Subscribe Directly

Subscribe Directly

This is just an example for explanation. This code will print 1,2,3,4,5. You can directly subscribe to publisher with sink.

Subscribe with AnyCancellable

AnyCancellable

By storing it on AnyCancellable, you can now reuse your subscription anywhere you want. Why should you store it on AnyCancellable? As I showed you previously, Any Swift Object or Data can be a publisher. You know what any means! any simply takes an ambiguous data type with similar types. So AnyCancellable means Any Publishers that can be cancelled.

Subscriber, The Most Common Usecase

Subscriber The Most Common Usecase

You probably have already seen this. The whole purpose of this is to store your subscription. Because normally when function finished its job, all values inside functions are gone. That’s why you store subscription as a global variable.

What if I have multiple publishers? (Optional For Beginner)

Multiple Publishers

Declare a global variable with Set<AnyCancellable>, and call chain function store.

Two Ways to cancel your subscriptions

Cancel your subscription

There are two ways to cancel your subscriptions. One is to call cancel(), another is to set it nil.

Operator

Combine Operators

Here comes the big one. The main reason for using combine or rxswift is that it helps you manipulate data so easily. Basically, before you receive the data stream, you can manipulate data.

Programmatically saying, before you call sink, you can call any number of operator functions before getting the result!

There are a whole bunch of pre-built methods. What you need to do is just search — apply.

Conclusion

After reading this tutorial several times, you can start using Combine. If you think it difficult, it becomes infinitely difficult, and if you think it easy, it becomes infinitely easy.

However, if what you want is not at the level of simply using the combine, a little more learning is needed to use it ‘well’. So I will write more about combine!

--

--

KD Knowledge Diet
KD Knowledge Diet

Written by KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

No responses yet