Dan
Dan

Reputation: 173

RxSwift design for async request

I'm new to RxSwift trying to find out the simplest way to achieve the following:

Model

let subject = ReplaySubject<[MyObject]>.create(bufferSize: 3)
var observable : Observable<[MyObject]>?

 init() {
        self.observable = subject
 }
 ...

 self?.insertFirstDataToDb(firstData){
   self?.api.getNextData(param,  success: { (data) -> Void in
    self?.insertNextDataToDb(firstData)

  })
  ... 
}

ViewController

override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // show spinner if data is not available yet
        self.myModel?.observable?
            .subscribe(onNext : { (additionalData) in
                self.updateMyTab(additionalData)
               // hide spinner
        })
        .disposed(by: self.disposeBag)
}

I suppose this can be done without using RxSwift, would there be any performance issues? Any suggestions how to solve this?

Upvotes: 0

Views: 880

Answers (1)

Timofey Solonin
Timofey Solonin

Reputation: 1403

To start and stop the spinner you can respectively use onSubscribed and onNext closures of the do operator.

To channel multiple requests one after another I would recommend do use flatMapLatest operator. For a good design you can develop a decorator for the request that will launch the spinner when the request is subscribed to.

Upvotes: 0

Related Questions