为什么即使我更改了值,我的地图 livedata 也没有被调用?
why my map livedata is not called even though I have change the value?
我是 MVVM 和 Android 架构组件的新手。所以我有这样的存储库
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
)
}
}
我有客户使用 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 获取数据 post。所以我假设因为 Livedata 发生了变化,它会触发 Transformations.map ,但是 count
的登录从未出现在我的 Logcat.
中
此计数仅在 RestaurantRepository
中是私有的,这只是为了测试,因为我仍在学习 livedata
根据docs
The transformations aren't calculated unless an observer is observing the returned LiveData object.
因此,您需要订阅 count
LiveData 才能使转换工作。
我是 MVVM 和 Android 架构组件的新手。所以我有这样的存储库
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
)
}
}
我有客户使用 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 获取数据 post。所以我假设因为 Livedata 发生了变化,它会触发 Transformations.map ,但是 count
的登录从未出现在我的 Logcat.
此计数仅在 RestaurantRepository
中是私有的,这只是为了测试,因为我仍在学习 livedata
根据docs
The transformations aren't calculated unless an observer is observing the returned LiveData object.
因此,您需要订阅 count
LiveData 才能使转换工作。