What is the difference between Observable, Completable and Single in RxJava

前端 未结 3 1185
陌清茗
陌清茗 2020-12-22 15:40

Can anyone please explain the difference between Observable, Completable and Single in RxJava with clear examples?

In which scenario we use one over the others?

3条回答
  •  执念已碎
    2020-12-22 16:28

    Single & SingleObserver

    Single always emits only one value or throws an error. Single always makes sure there is an emission.

    A use case of Single would be making a network call to get a response as the response will be fetched at once.

    Notice here, the SingleObserver doesn’t have onNext() to emit the data, instead the data will be received in the onSuccess(Note note) method.


    Completable & CompletableObserver

    Completable observable won’t emit any data instead it notifies the status of the task either success or failure. This observable can be used when you want to perform some task and not expect any value.

    A use case would be updating some data on the server by making a PUT request.

提交回复
热议问题