Rxjava: FlatMap with Completable

Created on 24 Mar 2017  路  6Comments  路  Source: ReactiveX/RxJava

Basically I want to have the option of converting an Observable from flatMap to use as Completable. I am using RxJava 1.2.1 and trying to migrate to RxJava 2. Following the lead by Jake, I am removing all the Observable declarations and replacing them with Completable. Here is the problem where I am stuck

 public Observable<Void> saveReminder(final Reminder reminder)
    {
        return Observable.fromEmitter((Emitter<DataSnapshot> dataSnapshotAsyncEmitter) ->
        {
            FirebaseApi.getReminderReference().addListenerForSingleValueEvent(new ValueEventListener()
            {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot)
                {
                    dataSnapshotAsyncEmitter.onNext(dataSnapshot);
                    dataSnapshotAsyncEmitter.onCompleted();
                }

                @Override
                public void onCancelled(DatabaseError databaseError)
                {
                    if (databaseError != null)
                    {
                        dataSnapshotAsyncEmitter.onError(databaseError.toException());
                    }
                }
            });
        }, Emitter.BackpressureMode.LATEST).observeOn(Schedulers.io()).flatMap(reminderCardsSnapshot ->
        {
            try
            {
                return Observable.just(saveData(reminder, reminderCardsSnapshot));
            } catch (Exception e)
            {
                return Observable.error(e);
            }
        });
    }

I need to replace Observable with Completable. Plz help

2.x Question

Most helpful comment

.flatMapCompletable(reminderCardsSnapshot ->
    Completable.fromAction(() -> saveData(reminder, reminderCardsSnapshot)))

As a general tip, I suggest you walk through each method in the classes so you can build a mental map on what's available.

All 6 comments

Which one?

@akarnokd I want to replace Observable to return Completable.
try { return Observable.just(saveData(reminder, reminderCardsSnapshot)); } catch (Exception e) { return Observable.error(e); }
How can the above method return Completable when it is wrapped inside flatMap which returns an Observable. Can you plz change the method above to return Completable?

.flatMapCompletable(reminderCardsSnapshot ->
    Completable.fromAction(() -> saveData(reminder, reminderCardsSnapshot)))

As a general tip, I suggest you walk through each method in the classes so you can build a mental map on what's available.

Agreed. But flatMapCompletable is available in RxJava2...do you know any way where we can achieve the same in RxJava 1.2.1?

Upgrade to 1.2.9.

@akarnokd Thanks 馃憤

Was this page helpful?
0 / 5 - 0 ratings