Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Disposables.create() in RxSwift

I'm learning RxSwift and I've come across the following pattern when creating Observables:

return Observable.create { observer in

    let disposable = Disposables.create()

    // Do some stuff with observer here

    return disposable
}

As far as I can tell the returned Disposable doesn't actually do anything, does it serve a purpose other than to meet the requirements of the API to return a Disposable?

Is there any scenario where you might need to return a configured Disposable?

I suppose the thing that's confusing me the most is that the returned Disposable seems separate from the implementation of the Observable being created, i.e. it's not assigned to any properties or passed anywhere it's just created and returned.

like image 492
mike Avatar asked Aug 31 '18 13:08

mike


People also ask

What is disposables RxSwift?

BooleanDisposable. Represents a disposable resource that can be checked for disposal status.

What is Onnext in RxSwift?

Called by the Observable when it emits an item. Takes as a parameter the item emitted by the Observable. Calls are referred to as emissions .

What is subscribe in RxSwift?

RxSwift: Subscribing to Observables It is the subscription that triggers an observable to begin emitting events until . error or . completed is emitted. Observing an observable is known as subscribing. Subscribe takes an escaping closure that takes an event of the supplied typed (that doesn't return anything).


2 Answers

There are two variations of the create method in relation to Disposables.

The first one, as Daniel mentioned, is used when you create a new Observable; you'll use the Disposables.create { ... } closure to "do cleanup", basically.

This is highly useful when using flatMapLatest, as your previous request will be disposed when a new ones comes in. Whenever it would be disposed, that "clean up" block will be called.

Observable<Int>.create { observer in
    let someRequest = doSomeLongRunningThing { result in
        observer.onNext(result)
        observer.onCompleted()
    }

    return Disposables.create {
        // How can I "cleanup" the process?
        // Cancel the request, for example.
        someRequest.cancel()
    }
}

The second variation of Disposables.create is used for an entirely different purpose - grouping several Disposable objects as a single disposable object (a CompositeDisposable).

For example:

let disposable1 = someAction()
let disposable2 = someOtherAction()

let compositeDisposable = Disposables.create(disposable1, disposable2)
like image 134
Shai Mishali Avatar answered Nov 16 '22 03:11

Shai Mishali


The Disposables.create function takes an optional closure. You should put any cancelation code in that closure. If you don't have any way to cancel, then the code is empty.

A good example is the wrapper around URLSession's dataTask method. In non-Rx code when you call URLRequest.shared.dataTask it returns a URLSessionDataTask object which can be used to cancel the network call. That object's cancel function gets called in the disposable.

Another common use is when you subscribe to some other observable from within your create closure. You then have to pass the disposable from that/those subscriptions by returning a Disposables.create(myDisposable) So that those subscriptions will get canceled properly when your Observable is disposed of.

like image 44
Daniel T. Avatar answered Nov 16 '22 03:11

Daniel T.