{"id":287,"date":"2018-10-12T06:06:01","date_gmt":"2018-10-12T06:06:01","guid":{"rendered":"https:\/\/nurlandroid.com\/?p=287"},"modified":"2021-04-01T07:59:00","modified_gmt":"2021-04-01T07:59:00","slug":"%d0%b2%d0%b8%d0%b4%d1%8b-observable","status":"publish","type":"post","link":"https:\/\/nurlandroid.com\/?p=287","title":{"rendered":"\u0412\u0438\u0434\u044b Observable"},"content":{"rendered":"\n<p>\u041a\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0431\u044b\u0432\u0430\u044e\u0442 Observable? \u041a\u043e\u0433\u0434\u0430 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>\u041a\u0430\u043a\u0438\u0435 \u0431\u044b\u0432\u0430\u044e\u0442 Observable &#8212; Observer?<\/strong><\/p>\n\n\n\n<p><strong>Observable<\/strong>\u00a0\u0431\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0438\u0434\u043e\u0432, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438 \u043a\u0430\u043a \u044d\u043c\u0438\u0442\u044f\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435. \u0412\u044b\u0431\u0438\u0440\u0430\u0442\u044c <strong>Observable\u00a0<\/strong>\u043d\u0430\u0434\u043e \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0418\u0442\u0430\u043a, \u0432 RxJava \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u0438\u0434\u044b\u00a0<strong>Observable:<\/strong>\u00a0<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Observable<\/strong><\/li><li><strong>Single<\/strong><\/li><li><strong>Maybe<\/strong><\/li><li><strong>Flowable<\/strong><\/li><li><strong>Completable<\/strong><strong><\/strong><\/li><\/ul>\n\n\n\n<p>\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0432\u0435\u0441 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 <strong>Observable<\/strong> \u043c\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u044e\u0449\u0438\u0439 \u0435\u043c\u0443 <strong>Observer<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Observer<\/strong><\/li><li><strong>SingleObservable<\/strong><\/li><li><strong>MaybeObservable<\/strong><\/li><li><strong>CompletableObserver<\/strong><strong><\/strong><\/li><\/ul>\n\n\n\n<p>\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b Observables \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0442\u0435\u043c \u043a\u0430\u043a \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043d\u0438 \u044d\u043c\u0438\u0442\u044f\u0442 \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>Observable<\/strong><\/td><td><strong>Observer<\/strong><\/td><td><strong>\u041a\u043e\u043b-\u0432\u043e \u044d\u043c\u0438\u0441\u0441\u0438\u0439<\/strong><\/td><\/tr><tr><td>Observable<\/td><td>Observer<\/td><td>\u041c\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e<\/td><\/tr><tr><td>Single<\/td><td>SingleObserver<\/td><td>\u041e\u0434\u043d\u0430 \u044d\u043c\u0438\u0441\u0441\u0438\u044f<\/td><\/tr><tr><td>Maybe<\/td><td>MaybeObserver<\/td><td>\u041e\u0434\u043d\u0430 \u044d\u043c\u0438\u0441\u0441\u0438\u044f \u0438\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e<\/td><\/tr><tr><td>Flowable<\/td><td>Observer<\/td><td>\u041c\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e<\/td><\/tr><tr><td>Completable<\/td><td>CompletableObserver<\/td><td>\u041d\u0438\u0447\u0435\u0433\u043e<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Observable &amp; Observer<\/strong><\/p>\n\n\n\n<p><strong>Observable<\/strong> \u043f\u043e\u0436\u0430\u043b\u0443\u0439 \u0441\u0430\u043c\u044b\u0439 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 observable(\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0435 \u0437\u0430 \u0442\u0430\u0432\u0442\u043e\u043b\u043e\u0433\u0438\u044e). <strong>Observable<\/strong> \u044d\u043c\u0438\u0442\u0438\u0442 \u043e\u0434\u0438\u043d \u0438 \u0431\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u0438\u0434\u0435\u0442\u044c \u043a\u0430\u043a <strong>observable<\/strong>\u00a0\u044d\u043c\u0438\u0442\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0434\u0438\u043d \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c. \u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u044d\u043c\u0438\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u0440\u0430\u0437\u0443. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443(\u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435), \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u044d\u043c\u0438\u0442\u0438\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u044d\u043c\u0438\u0442\u0438\u043c \u043e\u0434\u0438\u043d Note\nObservable&lt;Note>\n \n\/\/ \u044d\u043c\u0438\u0441\u0441\u0438\u044f \u0432\u0441\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u0440\u0430\u0437\u0443, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443\nObservable&lt;List&lt;Note>><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.os.Bundle;\nimport android.support.v7.app.AppCompatActivity;\nimport android.util.Log;\n \nimport java.util.ArrayList;\nimport java.util.List;\n \nimport info.androidhive.rxandroidexamples.R;\nimport info.androidhive.rxandroidexamples.observers.model.Note;\nimport io.reactivex.Observable;\nimport io.reactivex.ObservableEmitter;\nimport io.reactivex.ObservableOnSubscribe;\nimport io.reactivex.Observer;\nimport io.reactivex.android.schedulers.AndroidSchedulers;\nimport io.reactivex.disposables.Disposable;\nimport io.reactivex.schedulers.Schedulers;\n \npublic class ObserverActivity extends AppCompatActivity {\n \n    private static final String TAG = ObserverActivity.class.getSimpleName();\n    private Disposable disposable;\n \n    \/**\n     * Simple Observable emitting multiple Notes\n     * -\n     * Observable : Observer\n     *\/\n \n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_observer);\n \n        Observable&lt;Note> notesObservable = getNotesObservable();\n \n        Observer&lt;Note> notesObserver = getNotesObserver();\n \n        notesObservable.observeOn(Schedulers.io())\n                .subscribeOn(AndroidSchedulers.mainThread())\n                .subscribeWith(notesObserver);\n    }\n \n    private Observer&lt;Note> getNotesObserver() {\n        return new Observer&lt;Note>() {\n \n            @Override\n            public void onSubscribe(Disposable d) {\n                Log.d(TAG, \"onSubscribe\");\n                disposable = d;\n            }\n \n            @Override\n            public void onNext(Note note) {\n                Log.d(TAG, \"onNext: \" + note.getNote());\n            }\n \n            @Override\n            public void onError(Throwable e) {\n                Log.e(TAG, \"onError: \" + e.getMessage());\n            }\n \n            @Override\n            public void onComplete() {\n                Log.d(TAG, \"onComplete\");\n            }\n        };\n    }\n \n    private Observable&lt;Note> getNotesObservable() {\n        final List&lt;Note> notes = prepareNotes();\n \n        return Observable.create(new ObservableOnSubscribe&lt;Note>() {\n            @Override\n            public void subscribe(ObservableEmitter&lt;Note> emitter) throws Exception {\n                for (Note note : notes) {\n                    if (!emitter.isDisposed()) {\n                        emitter.onNext(note);\n                    }\n                }\n \n                \/\/ all notes are emitted\n                if (!emitter.isDisposed()) {\n                    emitter.onComplete();\n                }\n            }\n        });\n    }\n \n    private List&lt;Note> prepareNotes() {\n        List&lt;Note> notes = new ArrayList&lt;>();\n        notes.add(new Note(1, \"Buy tooth paste!\"));\n        notes.add(new Note(2, \"Call brother!\"));\n        notes.add(new Note(3, \"Watch Narcos tonight!\"));\n        notes.add(new Note(4, \"Pay power bill!\"));\n        return notes;\n    }\n \n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        disposable.dispose();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Note {\n    int id;\n    String note;\n \n    \/\/ getters an setters\n}<\/code><\/pre>\n\n\n\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0439 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u0434\u0430<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onSubscribe\nonNext: Buy tooth paste!\nonNext: Call brother!\nonNext: Watch Narcos tonight!\nonNext: Pay power bill!\nonComplete<\/code><\/pre>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<p> <strong>Single &amp; SingleObserver<\/strong><\/p>\n\n\n\n<p><strong>Single<\/strong> \u044d\u043c\u0438\u0442\u0438\u0442 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043b\u0438 \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443. \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <strong>Observable<\/strong> \u0441 \u0435\u0434\u0438\u043d\u043e\u0439 \u044d\u043c\u0438\u0441\u0441\u0438\u0435\u0439, \u043d\u043e <strong>Single<\/strong> \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u043d\u0430 \u044d\u043c\u0438\u0441\u0441\u0438\u044f. Single \u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n\n\n\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0438\u0436\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u044d\u043c\u0438\u0442\u0438\u0442 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 <strong>Note<\/strong>. \u0415\u0449\u0435 \u043e\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Single \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <strong>Note<\/strong> \u043f\u043e \u0435\u0433\u043e id.\u00a0 <br\/>\u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e Note \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043a\u0430\u043a Single \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u044d\u043c\u0438\u0442\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n\n\n\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e <strong>SingleObserver<\/strong> \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<strong>onNext()<\/strong>, \u0447\u0442\u043e\u0431\u044b \u044d\u043c\u0438\u0442\u0438\u0442\u044c\u00a0 \u0434\u0430\u043d\u043d\u044b\u0435, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u043c <strong>onSuccess (Note note)<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.os.Bundle;\nimport android.support.v7.app.AppCompatActivity;\nimport android.util.Log;\n \nimport info.androidhive.rxandroidexamples.R;\nimport info.androidhive.rxandroidexamples.observers.model.Note;\nimport io.reactivex.Single;\nimport io.reactivex.SingleEmitter;\nimport io.reactivex.SingleObserver;\nimport io.reactivex.SingleOnSubscribe;\nimport io.reactivex.android.schedulers.AndroidSchedulers;\nimport io.reactivex.disposables.Disposable;\nimport io.reactivex.schedulers.Schedulers;\n \npublic class SingleObserverActivity extends AppCompatActivity {\n \n    private static final String TAG = SingleObserverActivity.class.getSimpleName();\n    private Disposable disposable;\n \n    \/**\n     * Single Observable emitting single Note\n     * Single Observable is more useful in making network calls\n     * where you expect a single response object to be emitted\n     * -\n     * Single : SingleObserver\n     *\/\n \n    \/\/ TODO - link to Retrofit  tutorial\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_single_observer);\n \n        Single&lt;Note> noteObservable = getNoteObservable();\n \n        SingleObserver&lt;Note> singleObserver = getSingleObserver();\n \n        noteObservable\n                .observeOn(Schedulers.io())\n                .subscribeOn(AndroidSchedulers.mainThread())\n                .subscribe(singleObserver);\n \n    }\n \n    private SingleObserver&lt;Note> getSingleObserver() {\n        return new SingleObserver&lt;Note>() {\n            @Override\n            public void onSubscribe(Disposable d) {\n                Log.d(TAG, \"onSubscribe\");\n                disposable = d;\n            }\n \n            @Override\n            public void onSuccess(Note note) {\n                Log.e(TAG, \"onSuccess: \" + note.getNote());\n            }\n \n            @Override\n            public void onError(Throwable e) {\n                Log.d(TAG, \"onError: \" + e.getMessage());\n            }\n        };\n    }\n \n    private Single&lt;Note> getNoteObservable() {\n        return Single.create(new SingleOnSubscribe&lt;Note>() {\n            @Override\n            public void subscribe(SingleEmitter&lt;Note> emitter) throws Exception {\n                Note note = new Note(1, \"Buy milk!\");\n                emitter.onSuccess(note);\n            }\n        });\n    }\n \n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        disposable.dispose();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onSubscribe\nonSuccess: Buy milk!<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Maybe &amp; MaybeObserver<\/strong><\/p>\n\n\n\n<p>Maybe \u043c\u043e\u0436\u0435\u0442 \u044d\u043c\u0438\u0442\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u043d\u0435 \u044d\u043c\u0438\u0442\u0438\u0442\u044c.\u00a0\u041e\u043d \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043b\u0438\u0431\u043e \u0432\u044b\u0434\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u043b\u0438\u0431\u043e \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u044d\u0442\u0430\u043a\u0438\u0439 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 Optional.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.util.Log;\n \nimport info.androidhive.rxandroidexamples.R;\nimport info.androidhive.rxandroidexamples.observers.model.Note;\nimport io.reactivex.Maybe;\nimport io.reactivex.MaybeEmitter;\nimport io.reactivex.MaybeObserver;\nimport io.reactivex.MaybeOnSubscribe;\nimport io.reactivex.android.schedulers.AndroidSchedulers;\nimport io.reactivex.disposables.Disposable;\nimport io.reactivex.schedulers.Schedulers;\n \npublic class MaybeObserverActivity extends AppCompatActivity {\n \n    private static final String TAG = MaybeObserverActivity.class.getSimpleName();\n    private Disposable disposable;\n \n    \/**\n     * Consider an example getting a note from db using ID\n     * There is possibility of not finding the note by ID in the db\n     * In this situation, MayBe can be used\n     * -\n     * Maybe : MaybeObserver\n     *\/\n \n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_maybe_observer);\n \n        Maybe&lt;Note> noteObservable = getNoteObservable();\n \n        MaybeObserver&lt;Note> noteObserver = getNoteObserver();\n \n        noteObservable.subscribeOn(Schedulers.io())\n                .observeOn(AndroidSchedulers.mainThread())\n                .subscribe(noteObserver);\n    }\n \n    private MaybeObserver&lt;Note> getNoteObserver() {\n        return new MaybeObserver&lt;Note>() {\n            @Override\n            public void onSubscribe(Disposable d) {\n                disposable = d;\n            }\n \n            @Override\n            public void onSuccess(Note note) {\n                Log.d(TAG, \"onSuccess: \" + note.getNote());\n            }\n \n            @Override\n            public void onError(Throwable e) {\n                Log.e(TAG, \"onError: \" + e.getMessage());\n            }\n \n            @Override\n            public void onComplete() {\n                Log.e(TAG, \"onComplete\");\n            }\n        };\n    }\n \n    \/**\n     * Emits optional data (0 or 1 emission)\n     * But for now it emits 1 Note always\n     *\/\n    private Maybe&lt;Note> getNoteObservable() {\n        return Maybe.create(new MaybeOnSubscribe&lt;Note>() {\n            @Override\n            public void subscribe(MaybeEmitter&lt;Note> emitter) throws Exception {\n                Note note = new Note(1, \"Call brother!\");\n                if (!emitter.isDisposed()) {\n                    emitter.onSuccess(note);\n                }\n            }\n        });\n    }\n \n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        disposable.dispose();\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Completable &amp; CompletableObserver<\/strong><\/p>\n\n\n\n<p><strong>Completable\u00a0<\/strong> \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 void-\u043c\u0435\u0442\u043e\u0434. \u041e\u043d \u043b\u0438\u0431\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0434\u0430\u043d\u043d\u044b\u0445, \u043b\u0438\u0431\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0422\u043e \u0435\u0441\u0442\u044c \u044d\u0442\u043e \u043d\u0435\u043a\u0438\u0439 \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u0438 \u043e\u043d \u043b\u0438\u0431\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f, \u043b\u0438\u0431\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0441\u0431\u043e\u0435\u043c.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.util.Log;\n \nimport info.androidhive.rxandroidexamples.R;\nimport info.androidhive.rxandroidexamples.observers.model.Note;\nimport io.reactivex.Completable;\nimport io.reactivex.CompletableEmitter;\nimport io.reactivex.CompletableObserver;\nimport io.reactivex.CompletableOnSubscribe;\nimport io.reactivex.android.schedulers.AndroidSchedulers;\nimport io.reactivex.disposables.Disposable;\nimport io.reactivex.schedulers.Schedulers;\n \npublic class CompletableObserverActivity extends AppCompatActivity {\n \n    private static final String TAG = CompletableObserverActivity.class.getSimpleName();\n    private Disposable disposable;\n \n    \/**\n     * Completable won't emit any item, instead it returns\n     * Success or failure state\n     * Consider an example of making a PUT request to server to update\n     * something where you are not expecting any response but the\n     * success status\n     * -\n     * Completable : CompletableObserver\n     *\/\n    \/\/ TODO - link to Retrofit  tutorial\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_completable_observer);\n \n        Note note = new Note(1, \"Home work!\");\n \n        Completable completableObservable = updateNote(note);\n \n        CompletableObserver completableObserver = completableObserver();\n \n        completableObservable\n                .subscribeOn(Schedulers.io())\n                .observeOn(AndroidSchedulers.mainThread())\n                .subscribe(completableObserver);\n    }\n \n \n    \/**\n     * Assume this making PUT request to server to update the Note\n     *\/\n    private Completable updateNote(Note note) {\n        return Completable.create(new CompletableOnSubscribe() {\n            @Override\n            public void subscribe(CompletableEmitter emitter) throws Exception {\n                if (!emitter.isDisposed()) {\n                    Thread.sleep(1000);\n                    emitter.onComplete();\n                }\n            }\n        });\n    }\n \n    private CompletableObserver completableObserver() {\n        return new CompletableObserver() {\n            @Override\n            public void onSubscribe(Disposable d) {\n                Log.d(TAG, \"onSubscribe\");\n                disposable = d;\n            }\n \n            @Override\n            public void onComplete() {\n                Log.d(TAG, \"onComplete: Note updated successfully!\");\n            }\n \n            @Override\n            public void onError(Throwable e) {\n                Log.e(TAG, \"onError: \" + e.getMessage());\n            }\n        };\n    }\n \n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        disposable.dispose();\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Flowable &amp; Observer<\/strong><\/p>\n\n\n\n<p><strong>Flowable\u00a0<\/strong>\u0434\u043e\u043b\u0436\u0435\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 observable \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \/ \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c <strong>observer<\/strong>.\u00a0 \u0412 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439, <strong>Flowable<\/strong> \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 10000 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0438 <strong>observer<\/strong> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e.<\/p>\n\n\n\n<p>\u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 <strong>Flowable<\/strong> \u044d\u043c\u0438\u0442\u0438\u0442 \u0447\u0438\u0441\u043b\u0430 \u043e\u0442 1 \u0434\u043e 100, \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <strong>Reduce()<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0447\u0438\u0441\u0435\u043b \u0438 \u044d\u043c\u0438\u0442\u0438\u043d\u0433\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.os.Bundle;\nimport android.support.v7.app.AppCompatActivity;\nimport android.util.Log;\n \nimport info.androidhive.rxandroidexamples.R;\nimport io.reactivex.Flowable;\nimport io.reactivex.SingleObserver;\nimport io.reactivex.android.schedulers.AndroidSchedulers;\nimport io.reactivex.disposables.Disposable;\nimport io.reactivex.functions.BiFunction;\nimport io.reactivex.schedulers.Schedulers;\n \npublic class FlowableObserverActivity extends AppCompatActivity {\n \n    private static final String TAG = FlowableObserverActivity.class.getSimpleName();\n    private Disposable disposable;\n \n    \/**\n     * Simple example of Flowable just to show the syntax\n     * the use of Flowable is best explained when used with BackPressure\n     * Read the below link to know the best use cases to use Flowable operator\n     * https:\/\/github.com\/ReactiveX\/RxJava\/wiki\/What%27s-different-in-2.0#when-to-use-flowable\n     * -\n     * Flowable : SingleObserver\n     *\/\n \n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_flowable_observer);\n \n        Flowable&lt;Integer> flowableObservable = getFlowableObservable();\n \n        SingleObserver&lt;Integer> observer = getFlowableObserver();\n \n        flowableObservable\n                .subscribeOn(Schedulers.io())\n                .observeOn(AndroidSchedulers.mainThread())\n                .reduce(0, new BiFunction&lt;Integer, Integer, Integer>() {\n                    @Override\n                    public Integer apply(Integer result, Integer number) {\n                        \/\/Log.e(TAG, \"Result: \" + result + \", new number: \" + number);\n                        return result + number;\n                    }\n                })\n                .subscribe(observer);\n    }\n \n    private SingleObserver&lt;Integer> getFlowableObserver() {\n        return new SingleObserver&lt;Integer>() {\n            @Override\n            public void onSubscribe(Disposable d) {\n                Log.d(TAG, \"onSubscribe\");\n                disposable = d;\n            }\n \n            @Override\n            public void onSuccess(Integer integer) {\n                Log.d(TAG, \"onSuccess: \" + integer);\n            }\n \n            @Override\n            public void onError(Throwable e) {\n                Log.e(TAG, \"onError: \" + e.getMessage());\n            }\n        };\n    }\n \n    private Flowable&lt;Integer> getFlowableObservable() {\n        return Flowable.range(1, 100);\n    }\n \n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        disposable.dispose();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onSubscribe\nonSuccess: 5050<\/code><\/pre>\n\n\n\n<p><strong><\/strong><\/p>\n\n\n\n<p><strong>Observable<\/strong>\u00a0\u0431\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0438\u0434\u043e\u0432, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438 \u043a\u0430\u043a \u044d\u043c\u0438\u0442\u044f\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435. \u0412\u044b\u0431\u0438\u0440\u0430\u0442\u044c <strong>Observable\u00a0<\/strong>\u043d\u0430\u0434\u043e \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0418\u0442\u0430\u043a, \u0432 RxJava \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u0438\u0434\u044b\u00a0<strong>Observable:<\/strong>\u00a0<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Observable<\/strong><\/li><li><strong>Single<\/strong><\/li><li><strong>Maybe<\/strong><\/li><li><strong>Flowable<\/strong><\/li><li><strong>Completable<\/strong><strong><\/strong><\/li><\/ul>\n\n\n\n<p>\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0432\u0435\u0441 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 <strong>Observable<\/strong> \u043c\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u044e\u0449\u0438\u0439 \u0435\u043c\u0443 <strong>Observer<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Observer<\/strong><\/li><li><strong>SingleObservable<\/strong><\/li><li><strong>MaybeObservable<\/strong><\/li><li><strong>CompletableObserver<\/strong><strong><\/strong><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u041a\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0431\u044b\u0432\u0430\u044e\u0442 Observable? \u041a\u043e\u0433\u0434\u0430 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":1,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}