Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of actions with RxSwift

I'm using RxSwift to simply my code. For my current project I'd like to apply RxSwift's principles to a mess of completion blocks from the LayerKit:

layerClient.connectWithCompletion { (success, error) -> () in
  if (!success) {
     // Error     
  } else {
    layerClient.requestAuthenticationNonceWithCompletion { (nonce, error) -> () in
      // Even more blocks
    }
  }
}

I'm thinking about something like this:

// In extension
public func rx_connect() -> Observable<Bool> {
    return create { observer in

        self.connectWithCompletion { (success, error) -> ()in
            if (success) {
                observer.on(.Next(success))
                observer.on(.Completed)
            } else {
                observer.on(.Error(error))
            }
        }
        return NopDisposable.instance
    }
} 

public func rx_requestAuthenticationNonce() -> Observable<String> {
    // Same for annother method
}

// In AppDelegate
self.layerClient.rx_connect()
 .then() // requestAuthenticationNonceWithCompletion and use the nonce for next action
 .then()
 .subscribeNext(…
 .onError(… // To catch all errors

RxSwift does not have a then() method. Is there another way to do this chaining stuff or am I thinking wrong on how to use ReactiveX in general?

like image 306
Niklas Avatar asked Sep 23 '15 17:09

Niklas


People also ask

When should I use RxSwift?

RxSwift helps when you need to combine complex asynchronous chains. RxSwift also has types such as Subject, a kind of bridge between the imperative and declarative worlds. The subject can act as an Observable, and at the same time, it can be an Observer, i.e. accept objects and issue events.

What are RxSwift traits?

Traits are observables with a narrow set of behaviors. Traits are simply a wrapper struct with a single read-only Observable sequence property.

Should I use RxSwift or combine?

Combine Subjects are thread safe whereas RxSwift Subjects are not. In RxSwift you rarely need Subjects anyway, in Combine you need them constantly.

What is RxSwift and combine?

RxSwift and Combine are reactive programming solutions whose purpose is to handle asynchronous events. RxSwift is the most popular framework whereas Combine is Apple's recently introduced built-in solution.


1 Answers

Right, RxSwift doesn't have the then operator because the name is very risky.

then can be a lot of things in the Reactive Extensions world a map a flatMap or even a switchLatest, depending on the context.

In this case I would suggest to use a flatMap because that is going to be returned using a map is an Observable of Observables, that in most cases are hard to combine and manage. So I would do:

self.layerClient.rx_connect()
 .flatMap(){ _ in
   return rx_requestAuthenticationNonce()
             .catchError(displayError)
 }
 .subscribeNext(…
 .onError(… // To catch all errors

I would use flatMap instead of map in this case because I am then able to catch errors without terminating the sequence in case rx_requestAuthenticationNonce() fails.

Be careful in using the catchError operator, this operator will terminate the sequence after recovering and any extra event will be ignored after that.

like image 199
bontoJR Avatar answered Sep 20 '22 11:09

bontoJR