1

I have an ugly error with mediator live data... and I can´t understand the problem. I have one activity A which asks for messages to the backend, when I come back from the background I always have to refresh again if there is data, then always call in onResume() to get the info , my code is as follows

private var userSource: LiveData<Resource<List<Messages>>> = MutableLiveData()
private val _userMessages = MediatorLiveData<List<Messages>>()
val userMessages: LiveData<List<Messages>> get() = _userMessages

fun getMessages()= viewModelScope.launch(dispatchers.main) {
        _userMessages.removeSource(userSource) // We make sure there is only one source of livedata (allowing us properly refresh)
        withContext(dispatchers.io) {
            val token = session.getSessionUser().accessToken
            userSource =
                getUserMessages(token = token  )
        }
        _userMessages.addSource(userSource) {
            if (it.status == Resource.Status.SUCCESS){
                _userMessages.value = it.data
            }
            _isLoading.value = it.status
            if (it.status == Resource.Status.ERROR) _snackbarError.value =
                Event(R.string.TEXT_ALERT_SERVER_ERROR)
        }
    }

When I click on an item in the list, I go to another activity B which has a logic identical to the previous one but obviously with a different backend call. When I go back from activity B to activity A I have onActivityResult and I request the data for activity A again, everything works fine. The problem is the following, now I have had to add an activity C, which is navigated from activity B.

A --> B --> C
A <-- B <-- C 

This activity C has the same call logic as the previous activities. The problem is that when I pressed on back from activity C and go to B, Activity B, I get an error in Activity A.

ava.lang.IllegalArgumentException: This source was already added with the different observer
        at androidx.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:89)
        at com.makeclub.home.menu.messages.MessagesViewModel$getMessages$1.invokeSuspend(MessagesViewModel.kt:86)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I add breakpoints in Activity A onResume and onActivityResult but never is called!!

I don´t understand the problem...

Can anybody help me?

4

2 回答 2

0

For anyone looking for a solution to this question, in your fragment "override fun onStop()" method, remove the mediatorLiveData datasource.

override fun onStop() {
    super.onStop()
    viewMode.mediatorLiveDateObject.removeDataSource(dataSource)
}
于 2021-12-09T01:58:21.413 回答
0

不要在 OnResume 上添加它,将它们放在 Oncreate 中。

于 2020-12-17T21:52:36.003 回答