0

I am new to the Rx world so please bear with me.

My code is in Kotlin but a Java code will help also.

I have 2 methods, one of them loads tasks from the database, if there are tasks, I want to send them to the server.

fun getListFromDb(): Single<List<TaskEntity>> {
    return taskEntityDao.getAll()
}

 fun syncTasks(localTasks: TaskSyncRequest): Observable<ApiResponse<List<TaskEntity>>> {
    return taskServices.syncTasks(localTasks)
}

I know I can use flatmap to chain observables, but just couldn't get it working between Single and Observables.

  • 1
    You're looking for the flatMapObservable operator. Also, if this is an HTTP network call, why does it return an Observable?, it would make a clearer intention if it did return a Single instead. – Ahmed Ashraf Sep 05 '18 at 17:54
  • 1
    `Single` represents single item, `Observable` - sequence of item. Seems like your api shoud return "single". Anyway you can easly conver observable to single, for example with `singleOrError()` method – zella Sep 05 '18 at 17:56
  • Yes this is actually a retrofit call, and nearly all the examples which they were covering it were using observables not single, would it be better if I changed them to single ? if so, This will kinda fix my problem right now – SearchedBeforeAsking128 Sep 05 '18 at 18:16
  • Recommended reading: https://github.com/ReactiveX/RxJava#continuations – akarnokd Sep 06 '18 at 08:07

1 Answers1

2

Try to use flatMapObservable You can use this method for transform single to observable;

Vova
  • 956
  • 8
  • 22