T
T
Tsuzukeru2021-04-27 17:32:29
Android
Tsuzukeru, 2021-04-27 17:32:29

How to filter Single?

There is a method that returns Single>:

@GET("task-m-001/list.php")
    fun getItems(): Single<List<String>>


Inside the list are links to pictures. Some of them are broken and picasso won't load them. I need to filter them somehow. How to do it?

I need to turn Single> into a set of Observable, call the link. If it is working, then add it to the list, if it is broken, then skip it.

I tried to do it through flatMap, but even it doesn't work.

My method looks like this:

fun getItems(){
        _networkState.postValue(NetworkState.LOADING)

        try {
            compositeDisposable.add(
                apiService.getItems()
                    .observeOn(Schedulers.io())
                    .subscribeOn(Schedulers.io())
                    .subscribe({
                        val itemsResponse = itemsResponseConverter.getItemResponse(it)
                        _downloadedItemsResponse.postValue(itemsResponse)
                        _networkState.postValue(NetworkState.LOADED)
                    },
                        {
                            _networkState.postValue(NetworkState.ERROR)
                        })
            )
        }catch (e: Exception) {
            Log.e(TAG, e.message.toString())
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-04-27
@Tsuzukeru

Didn't see flatMap in your code.
But in general it will be like this

getItems()
.flatMapObservable { items ->
    Observable.fromIterable(items).switchMap { "прозвонить ссылку, что бы это не значило" }
}.toList()

The output will beSingle<List<String>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question