FrangSierra/RxFirebase

observeSingleValueEvent implementation prevents it to return empty child values

alexpfx opened this issue · 2 comments

a few days ago I posted this question in StackOverflow:

https://stackoverflow.com/questions/51887649/rxfirebase-observesinglevalueevent-implementation-prevents-it-to-return-empty

for this query I've made:


override fun isFavorite(beerId: String): Observable<Boolean> {
        var task = path.child(beerId)
        return RxFirebaseDatabase.observeSingleValueEvent(task) { t -> map(t) }.toObservable()
    }
    private fun map(t: DataSnapshot): Boolean {
        return (t.value ?: false) as Boolean
    }

It seems that the filter DATA_SNAPSHOT_EXISTENCE_PREDICAT is preventing the result from being mapped:

@NonNull
    public static <T> Maybe<T> observeSingleValueEvent(@NonNull Query query, @NonNull Function<? super DataSnapshot, ? extends T> mapper) {
        return observeSingleValueEvent(query).filter(DataSnapshotMapper.DATA_SNAPSHOT_EXISTENCE_PREDICATE).map(mapper);
    }

at the same time I do not know if I'm doing something wrong or could easily get around this problem somehow, I'm not so skillful in Rx.

You should use observeSingleValueEvent(query) method, and not the one that includes a mapping clause. If the dataSnapshot doesn't exists, the map will always cause an exception, that's why the filter is being used there.

To avoid this you should just use the default observeSingleValueEventmethod of the library.

override fun isFavorite(beerId: String): Observable<Boolean> {
        var task = path.child(beerId)
        return RxFirebaseDatabase.observeSingleValueEvent(task).map{ it.exists }.toObservable()
    }

Also, I answered you already on StackOverflow too ;)