为什么即使更改了值也没有调用地图实时数据? [英] why my map livedata is not called even though I have change the value?

本文介绍了为什么即使更改了值也没有调用地图实时数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM和Android体系结构组件的新手.所以我有这样的仓库

I am new in MVVM and Android Architecture component. so I have repository like this

object RestaurantRepository {

    val restaurants : LiveData<ArrayList<Restaurant>> = RestaurantClient.restaurants

    private var count : LiveData<Int> = Transformations.map(restaurants) {

        Log.d("debugLog","map")

        it.size
    }

    fun searchRestaurants(query: String, latitude:Double, longitude: Double) {

        mQuery = query

        RestaurantClient.searchRestaurants(
            query = query,
            latitude = latitude,
            longitude = longitude,
            start = 0
        )
    }

}

我有客户使用翻新来获取数据

and I have client that getting data using Retrofit

object RestaurantClient {

    val restaurants = MutableLiveData<ArrayList<Restaurant>>()


    private val restaurantService = RetrofitServiceGenerator.getInstance(RestaurantAPI::class.java)

    fun searchRestaurants(query: String, latitude:Double, longitude: Double, start: Int) {

        val call = restaurantService.searchRestaurants(
            radius = 2000,
            query = query,
            latitude = latitude,
            longitude = longitude,
            start = start,
            count = NUMBER_OF_DOCUMENT_PER_PAGE
        )



        call.enqueue(object: Callback<RestaurantListBaseResponse> {

            override fun onFailure(call: Call<RestaurantListBaseResponse>, t: Throwable) {

                // if there is an error while sending data to server or while parsing the data

            }

            override fun onResponse(call: Call<RestaurantListBaseResponse>, response: Response<RestaurantListBaseResponse>) {

                if (response.isSuccessful) {

                    val listOfRestaurants = response.body()!!.restaurants
                    val restos = ArrayList<Restaurant>()

                    for (i in listOfRestaurants ) {
                        restos.add(i.restaurant)
                    }

                    restaurants.postValue(restos)


            }

        })


    }

我相信,我从服务器获取数据并将其通过RestaurantClient中的restaurants MutableLiveData发布.因此我认为是因为Livedata发生了变化,所以它将触发Transformations.map,但是count中的登录永远不会出现在我的Logcat中.

I believe, I get the data from server and post it through restaurants MutableLiveData in my RestaurantClient. so I assume because there is a change in Livedata, then it will trigger the Transformations.map , but that log in count never appear in my Logcat.

此计数仅在RestaurantRepository中专用,仅用于测试,因为我仍在学习livedata

this count is private for only in RestaurantRepository , this is just for a test because I am still learning about livedata

推荐答案

根据 docs

The transformations aren't calculated unless an observer is observing the returned LiveData object.因此,您需要订阅count LiveData才能使Transformations正常工作.

The transformations aren't calculated unless an observer is observing the returned LiveData object. So, you need to subscribe to count LiveData to make Transformations work.

这篇关于为什么即使更改了值也没有调用地图实时数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆