Reputation: 2169
What i am trying to do is to convert the ArrayList<Model>
to MutableLiveData<ArrayList<Model>>
to be send as a return value. Though i am getting the ArrayList<Model>
result correctly,i failed miserably in posting the value to MutableLiveData<ArrayList<Model>>
.
This is what i am trying to do...
suspend fun getSeasonBodyWeight(): MutableLiveData<ArrayList<MSBodyWeight>> {
val x = MutableLiveData<ArrayList<MSBodyWeight>>()
val y:ArrayList<MSBodyWeight> = getBWeightCoroutine()
x.postValue(y)
Log.i(TAG, "Check ${y.size}")
Log.i(TAG, "Check ${x.value}")
return x
}
This is what i getting in Logcat
I/FirebaseConnect: Check 2
I/FirebaseConnect: Check null
So what am i doing wrong. Also how to convert ArrayList<Model>
to MutableLiveData<ArrayList<Model>>
I am trying to learn Kotlin.. Please bear with me if its a NOOB question.
Thanks
Upvotes: 1
Views: 1679
Reputation: 784
suspend fun getSeasonBodyWeight(): MutableLiveData<ArrayList<MSBodyWeight>> {
val x = MutableLiveData<ArrayList<MSBodyWeight>>()
x.value = arrayListOf();
val y:ArrayList<MSBodyWeight> = getBWeightCoroutine()
x.postValue(y)
Log.i(TAG, "Check ${y.size}")
Log.i(TAG, "Check ${x.value}")
return x
}
Upvotes: 0
Reputation: 9656
When using postValue
and if you check the source code you will find in the method description:
Posts a task to a main thread to set the given value.
Means that the value will not be immediately set, it starts a task to change it. If you mean to change the value immediately you should use:
x.value = y
The difference between the two is that you cannot call setValue
from a background thread, that meaning, if you are in a background thread you should call postValue
. If you are in the main thread setValue
may be used
Upvotes: 2