RXSWIFT: REACTIVE EXTENSIONS FOR SWIFT PART 2

In this second part of the blogpost we are going to apply ReactiveX in order to create a sample iOS application using RxSwift.

In the first part we explore the theory behind it in order to understand RxSwift, specifically ReactiveX. You can checkout the fist part here.

The application that we are going to build is a telephone directory. This application contains a table containing all the contacts the user added. It also gives the user the possibility to add a new contact.

We applied RxSwift to update the interface while validating the contact at every user input. We also applied RxSwift to bind our model, a list of contacts, with the table displaying them.

You can check the final result here:

giphy-2

As you can see as we are entering the input the label updates which remaining errors exist and enables/disables the “Save” button.

To achieve this result we created a observable for the name text field and a observable for the number text field.

        let name = nameTextField.rx.text.orEmpty

.observeOn(MainScheduler.instance)

 

let number = numberTextField.rx.text.orEmpty

.observeOn(MainScheduler.instance)

We then combined this two to create a Contact Observable.

        let contact = Observable.combineLatest(name, number) { (name, number) -> Contact in

return Contact(name: name, number: number)

}

At every update of this Contact we updated the UI.

        contact.subscribe(onNext: { contact in

self.updateUI(with: contact)

})

The validation of the contact and the present errors was external to the Observables.

You can check out the full project here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s