Quick short guide to implement RxSwift

Arnav Gupta
Aug 9, 2017 · 2 min read
Love for reactive programming

I have gone through many blogs and seen tutorials but no one has given a clear picture to how to implement RxSwift in your code.This is the only purpose I am writing this post as I believe in “Implementing things is the best way to learn things”.

First step is to go to RxSwift github repo & install it: https://github.com/ReactiveX/RxSwift

I will use cocoaPods for my example:

pod ‘RxSwift’pod ‘RxCocoa’

1: In your ViewController import RXSwift & import RxCocoa.

2: Every Login/Signup screens contain textfields and buttons.By the end of the blog you will be able to write all the functions required.

3. All the blocks or calls I am listing below is to be called in viewDidLoad.

For getting all the changes you are doing with your textfield text or it works similar to textfieldDidChange you can write:

textFieldEmail.rx.text.subscribe(onNext:{ text inprint(text)}).addDisposableTo(disposeBag)

NOTE : Everything you write with RxSwift is observable sequence. eg:textFieldEmail.rx.text

Now to receive all events emitted by observable sequence you have to subscribe to that sequence.ie

You subscribe to observable sequences by calling
subscribe(on:(Event<T>)-> ()).
The passed block will receive all events emitted by that sequence.

I have added addDisposableTo(disposeBag) to remove subscribtions or deinit them when they are not required.(disposeBag) is a variable you have to declare global for a viewController as you have to use it many times.

let disposeBag = DisposeBag()

This is the crux of RxSwift If you are able to understand this then it is well and good else just keep following the blog until you are done with this blog ,i bet you will be able to understand with time.

Now for textfield delegate functions you can do .Now all controlEvents of textfield are emitted by this block.You can specify which control events you want to use in array . Just 4 lines of code for using all the textfield delegate functions you want to use.

textfield.rx.controlEvent([.editingDidBegin,.editingDidEnd]).asObservable().subscribe(on:{_ in 
print("edit begin")
}).addDisposableTo(disposeBag)

Once you are done with textfields now you have to perform action on button click.For this we have tap on button and no need to create a button action.The underneath lines will work same as of button action.

btnSignUp.rx.tap.subscribe(onNext:{[weak self] _ in
// perform action you want to perform
}).addDisposableTo(disposeBag)

I hope that you will be able to create your login/signup controllers by using RxSwift.Next part of the series will contain playing with tableViews with RxSwift.And then afterwards you are somewhat comfortable in writing RxSwift we will move on to the actual power of RxSwift with MVVM.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade