The basics of RxSwift learning tutorial

foreword

In the process of iOS development, we have to face the processing of asynchronous events almost all the time. For example, button click, data saving, audio background playback, interactive animation display. These events do not have a specific timing, and they may even occur at the same time.

Although Apple provides asynchronous mechanisms such as notifications, proxies, GCD, and closures, these mechanisms lack a unified abstract expression. Additionally, these mechanisms are not clear and concise enough to deal with shared mutable data or state. Of course, that's not to say that writing elegant asynchronous code isn't practical. After all, the asynchronous mechanism of iOS is still very powerful compared to other platforms.

Fortunately, we can handle asynchronous code gracefully with RxSwift.

See the documentation for the advantages of RxSwift and why you should use it. No nonsense here.

Introduction to RxSwift

In fact, reactive programming is not a new concept, but it has received more attention from developers in recent years. It was first proposed by the giant, and its main purpose is to deal with complex UI asynchronous events and real-time application response. There are also reactive programming implementations in various language versions in the community, including: RxJS, RxKotlin, Rx.NET, RxScala, RxSwift. These libraries are only implemented differently, so there is no communication barrier between developers discussing application logic.

ADVERTISEMENT

RxSwift is a reactive programming implementation of the Swift language, which finds a good balance between traditional imperative programming and pure functional programming. By using immutable code definitions to process input asynchronously, RxSwift responds to events in a deterministically composable form.

In general, RxSwift has three main components: Observable, Operator, Scheduler. Let's introduce them one by one.

Observable

Observable<T> Classes are arguably the cornerstone of the entire RxSwift framework. It can trigger a series of event streams asynchronously and carry immutable state variables. Simply put: it allows an instance of a class to observe the value of another instance object over a period of time. For example, an observer can capture events triggered by all observable objects, thereby realizing real-time UI update or real-time data processing.

in Observable<T> 

ADVERTISEMENT

The class conforms to the ObservableType protocol. In addition, there are only three events that can be triggered by an Observable object:

 

    next event: This event, when fired, delivers the latest value of the observable to the observer. completed event: This event means that the normal end of the observable's life cycle will not continue to trigger the event. error event: This event indicates that an error occurred in the observable object causing the life cycle to terminate abnormally.

For an observable integer variable, the events it fires in an asynchronous environment can be depicted on the timeline as a sequence of events like this:

In addition, we can combine these three types of events to implement more complex business logic. At the same time, we can also use this mechanism to easily achieve code decoupling and data transfer between multiple objects without writing proxy or closure code.

Here, we have another point worth noting. That is, there are actually two types of observable sequences.

Finite observable sequences

ADVERTISEMENT

 

The sequence refers to those observables that will eventually end in a completed or error event. The most typical example is to make network requests through the API:

    Start a data request and prepare for data reception. After receiving the response from the server, start receiving data. If the server or network fails closes the request and triggers error handling. If everything is OK, the request data is processed and analyzed.

Here is the code for the Rx paradigm of a file download request:

API.download(file: "http://www...")
 .subscribe( onNext: { data in
      append data to temporary file },
    onError: { error in
      display error to user },
    onCompleted: {
      use downloaded file })
ADVERTISEMENT

The function in this code  API.download (file:) will create an  Observable<Data> instance object, and will continuously trigger the next event during the entire data reception process. We then save these fragment data to a temporary file in the next event. If there is an error in this process, we will display the error message to the user. If all goes well we will save the temporary file to the device. Finally, after the download is complete, we can perform the next logical processing in completed.

Infinite observable sequences

Unlike network tasks, UI and interaction events are infinite observation sequences. They don't have an explicit lifecycle endpoint. For example, we need to make layout modifications in real-time for possible device orientation rotations. The device's orientation rotation itself happens randomly and has the same life cycle as the application itself. So Rx also needs a mechanism to support this infinite sequence of observations.

In response to this situation, in RxSwift we can deal with the following code:

UIDevice.rx.orientation.subscribe(onNext: { current in
 switch current {
  case .landscape:
   re-arrange UI for landscape
  case .portrait:
   re-arrange UI for portrait
 }
})

operator

ObservableType and the implementation of the Observable class contain a large number of asynchronous processing methods, which are also called operators. Since these operators only perform asynchronous input processing and produce corresponding output, it does not have unwanted side effects on the application. In addition, because of the high degree of decoupling between operators, it is easy to combine them to achieve complex functions.

For example, for the device orientation rotation above, we can filter all cases and then further process some of the values.

UIDevice.rx.orientation
 .filter { value in
  return value != .landscape
 }
 .map { _ in
  return "Portrait is the best!"
 }
 .subscribe( onNext: { string in
  showAlert(text: string)
 })

In the code above, we first filter out all .landscape orientations and do nothing. Then, we convert the remaining portrait to the string Portrait is the best! . The entire processing flow is roughly as follows:

This functional operator allows us to flexibly combine more powerful functions.

Scheduler

Schedulers is a concept corresponding to GCD, but the former is more convenient to use. The predefined Schedulers in RxSwift are enough for developers to handle most programming scenarios.

For example, we can use the serial sequence SerialDispatchQueueScheduler to handle the next event, the ConcurrentDispatchQueueScheduler to run parallel file download tasks, and the OperationQueueScheduler to run an NSOperationQueue operation queue. You can even use different Schedulers types in different tasks of the same observer, as shown below:

We color-coded the tasks on the left, then on the right the tasks were split into different steps and placed in different Schedulers. For example, the Network subscription task is split into three steps and placed in Custom NSOperation Scheduler, Background Concurrent Scheduler, Main Thred Serial Scheduler in turn.

Replenish

It is worth noting that RxSwift does not impose hard and fast rules on the application architecture of the client. This means that we can introduce RxSwift into existing projects for responsive programming practices. Of course, there must be one of the existing frameworks that is most suitable for RxSwift, and it is MVVM. Because in MVVM we can directly bind some properties in VM to UI.

Also, RxSwift alone is not enough for iOS programming. RxSwift is just a reactive implementation of the Swift language, we also need a Cocoa-level implementation. Fortunately, RxCocoa exists here as a responsive complement to UIKit. The preceding device orientation code  UIDevice.rx.orientation is the application of RxCocoa.

Summarize

As the beginning of the series, this article introduces some basic concepts and components of RxSwift, and more related content will be brought later.

Well, the above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for computer playthings.

 

http://www.itread01.com/articles/1506448445.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325983548&siteId=291194637