RxSwift made easy for Swift

Swift Diaries
4 min readOct 11, 2020

Image from: 1*jsf_n_Z29YNxm0TzgE5aTw@2x.png

It’s true that Combine framework is the reactive approach to be used if you are a die hard fan of Apple’s APIs. But RxSwift is still widely used. So thought of summing up some essentials to use when developing apps using Swift.

You can find the project related to this article here. Feel free to download and play around with the code.

I’ll describe how to populate some data fetched from a RestAPI in a UITableView reactively. Further more I’ll show how to use a UISegmentedControl to fetch data according to users preference. You will understand with this screenshot, what I will be discussing.

What we will be discussing at a glance!

I’ll be using an API provided by OpenBreweryDB.

There is a UISegmentedControl to choose within ‘Micro’, ‘Large’ or ‘Contract’ breweries. And a UITableView to populate the fetched data. I am using MVVM pattern in coding.

This is the model used to define the Breweries fetched from the API.

Model — Brewery

In the storyboard, in a UIViewController add a UITableView, UISegmentedControl and a UILabel which will be used later to show whether data is empty. For more UX, I’ve used an UIActivityIndicator too. You may add it also if you wish. Make connections to the ViewController class. To show details of breweries I have designed a separate UITableViewCell for more clarity.

UITableViewCell — BreweryTableViewCell

In the ViewModel I’ve declared an Enum called BreweryType which confirms to String protocol.

Enum — BreweryType

These are the types that will be used in the UISegmentedControl to fetch breweries of these types when user choose different segments.

Method — fetchBreweries

This method will fetch the breweries according to the provided type. And store in a variable called ‘breweries’ as in line #33.

BehaviorRelays — Variables to implement Rx

Here BehaviorRelay is an Observable type provided by RxSwift. You can use a BehaviorRelay to listen for some events. Will discuss it in a while. I’ve declared two variables to store the breweries which are fetched from the fetch method and to store the currently selected type in the segmented control. As a default value I’ve used ‘Micro’ as the selected type. Once the fetch method is completed, the fetched breweries are stored in the ‘breweries’ variable of type BehaviorRelay of type array of ‘Brewery’.

You can pass any type to BehaviorRelay. It expects a generic type. Also you can pass optionals too.

var breweries = BehaviorRelay<[Brewery]?>(value: nil)

So we have covered the ViewModel part where the data is fetched and stored in variables. Now we’ll use those stored values in the ViewController to display in the UI.

DisposeBag

In the ViewController class in line #20 you can see a variable called bag. It is a Dispose bag, which handles the deintializations. It can be explained as a Rx mechanism similar to ARC which manages resources. We have to remind to dispose all Rx that we use in our code to minimise memory leaks.

Now I’ll explain what I’ve done in the method called addObservers. It contains all the magic!

Delegate — Set UITableView delegate

If you need any UITableViewDelegate methods to be used, you can set delegate as above. Here I have used because I’ve used heightForRowAt method.

Bind — this binds the data to UITableView

This one method reduces lots of code line. If we use normal datasource methods, we have to set numberOfRowsInSection and cellForRowAt methods. This way we can do it using one methods. And now we don’t need to reload the tableView when data is loaded. Once data is set to data variable(breweries here) in the viewModel, it automatically reloads behind the scenes. This bind method is available because the breweries is a BehaviorRelay.

Segmented Control

Here I’ve used a listener to, listen whether a value is changed in the segment controller. Here it will change the selectedBreweryType value each an every time segment control is changed. In the line #65, it is listening to selectedBreweryType whether it’s value is changed. Once it’s changed, it will execute, fetchBreweries() method.

Empty label — shows whether data is available

With the above code, it will check whether the breweries array is empty. If it is empty, it will display the empty label as follows.

Empty label

I hope this article gave you an idea to use RxSwift to make your code more cleaner and neat with less code.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Swift Diaries
Swift Diaries

Written by Swift Diaries

I am an iOS developer and thought of writing day today Swift things for the community :)

No responses yet

Write a response