0

In my repository class I am calling webservice. If query is not null then returning the result as flowable, if query is null then return null . Then in my ViewModel I am converting the flowable to LiveData.

Below is the code snippet:

In myRepository class:

fun fetchToDosFromServer(fullQueryString: String?)
        : Flowable<GitResult>? 
{
    if(fullQueryString.length>0)
    { //call webservice- which return observable<GitResult>
       var returnedResult = myWebServiceCall()
        return returnedData.toFlowable(BackpressureStrategy.BUFFER)
       }

    else 
        return null

}

In myViewModel class:

 fun getReposFromServer()
 {
    val resultFromApiCall_flowable : Flowable<GitResult>? =  mainRepository.fetchToDosFromServer("q=2")

    val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)
 }

but its showing compile time error when trying to convert flowable to LiveData in myViewModel class. In the line:

LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)

the error is:

Error: required Flowable , found Flowable?

how do I solve it?

Rashedul Rough
  • 89
  • 1
  • 10
  • @coroutineDispatcher : doen't work – Rashedul Rough Dec 17 '19 at 15:54
  • 1
    `val resultFromApiCall_flowable : Flowable? = mainRepository.fetchToDosFromServer("q=2")` is optional. So you can force cast with a `val resultFromApiCall_flowable : Flowable! = ..` Or you make a null check. `resultFromApiCall_flowable?.let { val source: LiveData = LiveDataReactiveStreams.fromPublisher(it) }` – kuzdu Dec 17 '19 at 15:55
  • @kuzdu : I used the second option and it worked. thanks. But i have another confusion.. if I use `if(resultFromApiCall_flowable!=null) { val source: LiveData = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable) }` then the error goes away too. But is it same as `xx?.let{...}` – Rashedul Rough Dec 17 '19 at 16:45
  • It is, but you can handle `else` more conveniently here. – Alexey Romanov Dec 17 '19 at 17:01

2 Answers2

0

Your flow is optional.

So if you make a null check it should work.

val resultFromApiCall_flowable: Flowable<GitResult>? = mainRepository.fetchToDosFromServer("q=2")

if (resultFromApiCall_flowable != null) {
 val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)
}

This is the same like

resultFromApiCall_flowable?.let { 
 val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(it)
}

Anyway, you can use both, but it is not the same.

Have a look here: let vs if not null

If you like to handle the else case:

b?.let {
    // If b is not null.
} ?: run {
    // If b is null.
}

From https://stackoverflow.com/a/46726337/4420355 by marstran

kuzdu
  • 7,124
  • 1
  • 51
  • 69
0

You've already got an answer how to handle it directly, but I suggest considering a different direction. Simply don't make the result nullable, and return Flowable.empty() in that case instead. And maybe don't accept String? either; do you actually want to call fetchToDosFromServer(null) in some cases?

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • if I return `Flowable.empty()` then how do I check when its returned empty? – Rashedul Rough Dec 17 '19 at 21:39
  • In most cases you don't want to check; just treat it as any other returned `Flowable`. If you do want to distinguish this situation, this isn't a good solution. But why treat `fetchToDosFromServer("")` differently from `fetchToDosFromServer(anyInvalidQueryString)`? – Alexey Romanov Dec 18 '19 at 05:40
  • Of course, this suggests returning `Flowable.error()` or throwing an exception instead of `Flowable.empty()`; whatever `fetchToDosFromServer` does for invalid query strings. – Alexey Romanov Dec 18 '19 at 05:43