[RxSwift] What are Subjects?

봄인·2023년 3월 5일
0

About-RxSwift

목록 보기
2/5
post-thumbnail

What is Subjects?

Subjects are very special because they function like observables as well as observer.


Publish Subjects

Subjects, can be subscribed and it can emit events. It doesn't need a default value to start with.

let subject = PublishSubject<String>()

We can create a subject by the code above. As we can see, it doesn't require initial value. After creating the subject, let's create some events.

subject.onNext("Issue Occured 1")

In this case, this subject can only emit events of String, so we created a String value event.

We also have to make a subscription like the following code.

subject.subscribe { event in
    print(event)
}

If we run all three codes above, nothing will happen because there were no events emitted after the subscription.

In order to work properly, we have to emit events after the subscription.

let subject = PublishSubject<String>()

subject.onNext("Issue Occured 1")

subject.subscribe { event in
    print(event)
}

subject.onNext("Issue Occured 2")

The result of the code above will be the following:

next(Issue Occured 2)

As mentioned above, the events should be emitted after the subscription of subject in order to work properly.

Also, dispose of subject should happen at the last because if we dispose a subject and try to emit events, it will not work.


Behavior Subjects

Behavior subjects are very similar to publish subjects but, there is a difference. Behavior subject requires the initial value.

let subject = BehaviorSubject(value: "Initial Value")

When we subscribe this subject, it emits the last value that was issued before subscription.

let subject = BehaviorSubject(value: "Initial Value")

subject.onNext("Last Issue")

subject.subscribe { event in
    print(event)
}

So, the result of the code above will be like the following:

next(Last Issue)

Replay Subjects

As the name of this subject, it basically replays the subject based on the buffer size that we are going to set.

let subject = ReplaySubject<String>.create(bufferSize: 2)

subject.onNext("Issue 1")
subject.onNext("Issue 2")
subject.onNext("Issue 3")

subject.subscribe {
    print($0)
}

This replay subject will emit the last two events(due to the bufferSize: 2) so, the result will look like this:

next(Issue 2)
next(Issue 3)

Before getting started with 'Behavior Relay', RxSwift doesn't include BehaviorRelay so we have to pod install RxCocoa. FOLLOW THIS

We can check that RxCocoa is installed properly. Before using BehaviorRelay, make sure that 'RxCocoa' is imported.


BehaviorRelay

BehaviorRelay is an alternative of using 'Variables'.
Variables were deprecated since RxSwift 5.

let relay = BehaviorRelay(value: "Initial Value")

relay.asObservable()
    .subscribe {
        print($0)
    }

//relay.value = "Hello World"

relay.accept("Hello World")

As the code above, BehaviorRelay needs an initial value. It needs to be converted as observables in order to subscribe. We can not change the value of it since it's immutable(read-only value). However, we can use the function 'accept' which takes in the new value.

Let's figure out how to change the value when the value is an Array instead of simple String.

let relay = BehaviorRelay(value: ["Item 1"])

relay.accept(relay.value + ["Item 2"])

relay.asObservable()
    .subscribe {
        print($0)
    }

We can add certain arrays like the code above, this will give result like the following:

next(["Item 1", "Item 2"])

There is another way to do this.

let relay = BehaviorRelay(value: ["Item 1"])

var value = relay.value
value.append("Item 2")
value.append("Item 3")

relay.accept(value)

relay.asObservable()
    .subscribe {
        print($0)
    }

By creating a new variable called 'value' that contains the original array and append items to this 'value'. Use accept to change the BehaviorRelay's array value to 'value' from the initial array.

profile
ᕙ(•̀‸•́‶)ᕗ

2개의 댓글

comment-user-thumbnail
2023년 4월 5일

who need help improving their coding skills bubble slides, daily game solve cross

답글 달기
comment-user-thumbnail
2023년 8월 18일

color blind test it basically replays the subject based on the buffer size that we are going to set.

답글 달기