Well, In these existence  many programming languages and happening of many real time events(occurring in asynchronous and synchronous manner). It is also called FRP- Functional Reactive Programming. The reason it is called functional because It goes with the programming flow, cell-oriented  with its data stream.

This clearly means that it would be easy to express the change in static and dynamic data streams. iOS app development nowadays has a plenty of real time events which enables  best user experience. So, we need something like Reactive Programming to make a proper dealing with it.

For example, In C language when we make an expression like  total= sum+avg ; first , it evaluates with sum and avg and then the value is assigned to the total . But , in Reactive Programming, the whenever the values of sum and avg changes, the value of total is automatically updated, we don’t need a program to re-execute.

The other example is Model-View-Controller Architecture, Reactive Programming helps us in the change of associated Model which is automatically reflected to view , and vice versa. Well, coding with Reactive Programming is more concise. So we can say that FRP is totally opposite from MVC.

There are many programming languages comes with Rx, like java, android, swift. But here We will be focusing on Rx with Swift development.

Let’s take a look on it:

To declare a variable in Swift. It is represented as:

let name  = “John”

 

So, to declare a variable in RxSwift , we can do like this way :

let name: Variable<String> = Variable("John")

Now, that  variable name is of class variable which is already in RxSwift library.

So here we can see that John value is wrapped in variable , that’s why we can still access its value with calling  name.value

Now, we are calling the function with  our variable named and binding that function to another function to the result, passing in the view element that we are binding to.

name.fetchTo()

.getTo(nameLabel.rx.text)

In this particular case, we are binding to the text property of a UILabel named nameLabel. However, the getTo() function also allows us to bind to other properties of views, such as backgroundColor.

Lastly, We just have to initialize a class object. Even the View Controller is deallocated, the bind data which is created between data and views would be Prolonged/Persists. The object of class will make sure that the bindings are also deallocated when the View Controller is deallocated.

let   empInfo = Employee()

In order to add the binding to the empInfo , we have to call one last function on the binding. Here is what the final binding should look like:

name.fetchTo()

.getTo(nameLabel.rx.text)

.addInfoTo(empInfo)

 

Lastly, in order for your views to change, you need to change the data! The variable in this tutorial changes based on text input from a UIAlertController, we’ll implement the code that changes the view.

Normally, action closure might look like this:

let okAction = UIAlertAction(title: "OK", style: .default) { action in

          if let textInput = alert.textFields?.first?.text {

              self.name = textInput

              self.nameLabel.text = self.name

         }

}

 

Note: we have to update our views as well as our data. When we bind our views to the data, we don’t have to update the views. The binding implies it automatically updates! All we have to do is change the data itself.

let okAction = UIAlertAction(title: "OK", style: .default) { action in

          if let textInput = alert.textFields?.first?.text {

              self.name.value = textInput

          }

}

 

Remember that the type of name is Variable—meaning the data(“John”) is enclosed in a wrapper. We have to update the data , which affects at its value property , rather than updating the name itself.

Somehow, we have come to know that Reactive Programming is Multi-Platform Standard. RxSwift library completely uses Swift development and turns into a new way which was difficult to handle asynchronous code in Swift.

Suggestion of books to refer Reactive Programming:

Reactive Programming with Swift : Cecil Costa

RxSwift: Reactive Programming with Swift : raywenderlich  Team,

Share your thoughts with us by tweeting us @letsnurture. Write your suggestions and queries at info@letsnurture.com.

Share Button
Want to work with us? We're hiring!