1

I'm trying to get the data in the observe, but it doesn't work because the data doesn't arrive after the "postvalue" method is triggered in subscribe. What am I doing wrong?

in fragment

private fun initMapRoutes(mapRoutes: RecyclerView) {
    val m1 = MapRoute("1", "#ff00dd")
    val mapRouteAdapter = MapRouteAdapter(arrayOf(m1))
    mapRoutes.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
    mapRoutes.adapter = mapRouteAdapter

    val viewModel = ViewModelProviders.of(activity!!).get(OnlineTagiltramViewModel::class.java)
    viewModel.getMapTrams().observe(this, Observer { data -> onMapTramsReceived(data) })
    viewModel.loadMapTrams()
}

fun onMapTramsReceived(data: List<MapTram>?) {
    if (data == null || data.isEmpty()) {
        return
    }


}

in viewmodel

private var onlineTagiltramInteractor: OnlineTagiltramInteractor? = null

private var onlineTagiltramDisposable: Disposable? = null


val mapTrams: MutableLiveData<List<MapTram>> = MutableLiveData()

fun getMapTrams() : LiveData<List<MapTram>> {
    return mapTrams
}

init {
    this.onlineTagiltramInteractor = OnlineTagiltramInteractor()
}

fun loadMapTrams() {
    removeDisposable(onlineTagiltramDisposable)
    onlineTagiltramDisposable = onlineTagiltramInteractor
            ?.loadData()?.subscribe( { data -> mapTrams.postValue(data) },
                    {throwable -> Log.e("s", throwable.message)})
    addDisposable(onlineTagiltramDisposable!!)
}

in interactor

class OnlineTagiltramInteractor {

fun loadData() : Flowable<List<MapTram>> {
    val api = RetrofitFactory.instance.onlineTagiltramApiService

    return Flowable.interval(2, TimeUnit.SECONDS)
            .observeOn(Schedulers.io())
            .flatMap { api.getMapTrams() }
}

method onMapTramsReceived is not called

Nikitc
  • 795
  • 1
  • 6
  • 12
  • and you are 100% sure that `postValue` was called? tried using `Log.d` before calling `postValue`? – pskink Sep 09 '18 at 06:51
  • Why is `OnlineTagiltramInteractor` nullable? – EpicPandaForce Sep 09 '18 at 08:45
  • @pskink Yes, I checked in the debugger, I receive my data – Nikitc Sep 09 '18 at 09:11
  • @EpicPandaForce Good ask, i edit my code, but it didn't help – Nikitc Sep 09 '18 at 09:11
  • maybe the problem is that you need to specify the main thread in the flowable method? – Nikitc Sep 09 '18 at 09:12
  • @Nikitc re. "need to specify the main thread in the flowable method" - I think the fact that you're using `postValue` (instead of `setValue`) should mean that you don't need to be on main thread when calling it. – John O'Reilly Sep 09 '18 at 09:15
  • if body of the method `loadMapTrams` replace on `mapTrams.setvalue(arrayListOf(MapTram()))`, i receive my data in fragment, that's weird. But if I write `mapTrams.postValue(arrayListOf(MapTram()))` i not receive my data – Nikitc Sep 09 '18 at 09:37
  • Wondering if you're running in to something like https://stackoverflow.com/questions/51031062/android-livedata-not-receiving-all-notifications/51032084 ...am def seeing good number of reports in SO and elsewhere about issues like you're having with `postValue` – John O'Reilly Sep 09 '18 at 09:44

0 Answers0