For Arranging the concept of RxSwift, I decided to write series of Understanding rxswift
posting.
Why do developers use RxSwift?
When developer develops app, it is neccessary to develope app Reactively
.
Swift supports @escaping
handler to catch the end point of asynchoronous task.
For making task asynchoronously, we should use Dispatchqueue
.
willSet
and didSet
also makes observing specific variable reactively.
These things are all you need to know and it is neccesary for reactive programming.
However, you can make reactive programming just by using RxSwift.
In my opinion, advantage of using RxSwift is..
RxSwift makes reactive code readable and short. ( You don’t need to use those Dispatchqueue
, willSet
, and DidSet
)
RxSwift operators make stream extensive.
Now, let us know the concept of RxSwift which supports these advantages!
Concept
For understanding easily, I can show a youtube example.
When a BJ create a personal channel, then it is Observable.
Then, a video viewer can be a observer if he subscribes specfific video channel(Observable)
After video viewer(Observer) subscribes a channel of BJ(Observable), whenever new video of subscribed channel is uploaded, viewer can see the video from his recommanded video part from now on.
Here, video is an event which is sending from channel(Observable) to subsrcibed viewer(Observer).
Therefore, an video (event of Observable) is shown on viewers(Observer), viewers(Observer) can action for each videos(event of subscribed Observable).
In ReactiveX, an Observer subscribes an Observable, and then observer reacts whatever item or sequence of items the Observable emits.
Example of Observable and Observer.
It is difficult to understand both Observable and Observer only with concept.
Let us look example from now on.
Observer
checkIsEvenNumberObservable(items: [2,4,6,7,10])
.subscribe{ event in
switch event {
case .next(let value):
print(value)
case .error(let error):
print(error)
case .completed:
print("completed")
}
}.disposed(by: bag)
You know now, Observer subscribes Observable.
Closure after .subscribe
method is Observer.
By forking switch cases (next, error, completed), Observer can implement actions for each cases.
print
is action here.
Observable
We can see Observable now. You also can see observer parameter in Observable.
Because we can know it from the internal code of RxSwift (you do not need to know it right now), observer is closure after .subscribe
method from previous code.
func checkIsEvenNumberObservable(items: [Int]) -> Observable<Int> {
return Observable<Int>.create{ observer -> Disposable in
for i in items {
if i % 2 == 0 {
observer.onNext(i)
} else {
observer.onError(NSError(domain: "ERROR: value is not even.", code: 0, userInfo: nil))
}
sleep(1)
}
observer.onCompleted()
return Disposables.create()
}
From the above explaination, we can predict each .onNext(i)
, .onCompleted()
, .onCompleted()
sent to observer as event.
We studied internal flow of rxswift between observable and observer.
Last thing what we should know is just each behavior protocol for sending event. There are onNext, onError, and onComplete
onNext
onNext is stream which emits one or sequences. Because Observer still subscribes Observable.
Observer can do some action depend on events from Observabe.
onError
When Observerble gets error, it emits error and makes Observerble stream stopped.
It is important to know you that stopped emittion by error is not same with completion.
Stream is not completed, and just stopped.
onComplete
Observable emits event which means completion of stream, it is also same meaning that there are no more next event to emit from Observable.
Conclusion
We will study Disposable
next time.
Feel free to ask me if you have wondering.
Hope you guys get helped from this posting.