As a beginner, things you should know before using RxSwift.

Bhoomi Prajapati
3 min readMay 24, 2020

--

In the past few years you must have heard of Reactive programming and the Rx term a lot. As the RxSwift is gaining a lot of popularity all are blindly going with the RxSwift approach in their projects. Well, do not use it in simple applications. Here I am going to explain in which scenarios the RxSwift and MVVM approach would help.

If you have a simple application with a majority of UIView used, your application is much UI centric than Data then don’t go for the RxSwift approach. RxSwift will somehow add a dependency on your project and using more dependencies in a project is not a good practice. For such applications, the MVC with the Swift approach is the best.

If you have such requirements where depending on the change of one data, you have to change many things. For example, you have a bunch of data of a particular use displayed on a UITextField. What we want is if we make a change in UITextField it automatically changes the user’s data and vice versa. In that case, the traditional swift approach will lead to a lot of lines of code. So here, RxSwift can be used easily. All you have to do is, bind the UITextField with an observable and you’re good to go.

Another is when you have to use UITableView in your project a lot. With the help of RxDataSources you can easily manage your UITableView with fewer lines of code.

In swift, we create a notification add an observer to it, and then add the handler. RxSwift’s observables kind of work as those notifications.

Now that you know that RxSwift can be used when you want your app to work reactively. Let me put some common mistakes that you need to avoid while using RxSwift.

  1. Dispose of the observables correctly.

Dispose observables when you no longer need them. Otherwise, it will lead to memory leaks.

2. Using DisposeBag in UITableViewCell

When you create a UITableViewCell and add it to your TableView. All are most likely to add the DisposeBag of the UIViewController that contains the UITableView. In such cases, you’ll see the unwanted behavior. So, always use UITableViewCell’s DisposeBag when you want to perform changes on the cell’s properties.

3. Subscribing the Observable on the background thread

This is the most common mistake that every beginner does while using RxSwift. When you want to perform some UI related task do not just subscribe to the observable. For example, you have made an API call and as you get the response you want to show the pop-up message. Here, when you get the response in the background so you need to subscribe to the Observable on MainScheduler. If you don’t then your app might get crashed.

4. Binding UITableView in ViewWillAppear

This is also a very common mistake, You must bind the UITableView in the viewDidLoad method. If you do this is viewWillAppear your app will get crashed. When you open the screen for the first time this will work fine but when you push the next screen and come back, viewWillAppear will be called the TableView is already bound with the observable and still you’re still trying to bind that so your app will crash. If you are willing to bind the TableView in viewWillAppear then set the DataSource to nil while your view is disappearing.

There are so many things in RxSwift but these are those that I have faced a lot in the beginning. I hope this will help you. Happy Coding!

--

--

No responses yet