0

I have a scenario to modify each Object of the Single<List<Object>> refer to this. What I want to ask is how to set properties to each item with other Single/Observable result. I'm not sure with my method setProps() is correct. I got error when the result of the propsA/propsB is empty/null, and not sure how to handle it correctly.

override fun getObjects(): Single<List<Object>> {
    return db.objectDao().getObjectByUid(authPref.getUid())
            .flattenAsObservable { items -> items }
            .flatMap { setProps(it) }
            .toList()
}

private fun setProps(obj: Object): Observable<Object> {
    val propsA = db.propsADao().getProps(obj.id) // Single<List<A>>
    val propsB = db.probsBDao().getProps(obj.id) // Single<B>
  
    val result = propsA.flatMap { a ->
          propsB.flatMap {
              obj.propsA = a
              obj.propsB = it
              Single.just(obj)
          }
     }

    return result.toObservable()
}
etomun
  • 134
  • 6
  • 1
    Are you using `RxJava2`? if so, `RxJava2` Does not allow `null` values to be emitted. – Kalpesh Patel May 03 '20 at 14:15
  • yes I'm using RxJava2. What if the result of db query (ie from propsA) is empty? – etomun May 03 '20 at 14:33
  • I tested my code and it's works as expected but I'm not sure how to handle if the two other query is empty – etomun May 03 '20 at 14:43
  • empty list is fine. Only null values are not allowed. > "I'm not sure how to handle if the two other query is empty" Do you mean `db.propsADao()` and `db.propsBDao()`? – Kalpesh Patel May 03 '20 at 15:11
  • db.propsADao().getProps(obj.id) return empty rows, and so on with propB – etomun May 03 '20 at 15:27
  • Since `RxJava2` does not allow null values. Make sure that any observable does not return `null` values. If you think that a Observable can emit null values, then wrap it in Optional object at source. – Kalpesh Patel May 03 '20 at 16:22
  • 1
    https://medium.com/@joshfein/handling-null-in-rxjava-2-0-10abd72afa0b for your reference. – Kalpesh Patel May 03 '20 at 16:24

0 Answers0