gpt4 book ai didi

java - MediatorLiveData 多次调用观察者错误

转载 作者:行者123 更新时间:2023-12-02 10:44:25 25 4
gpt4 key购买 nike

  • 我正在开发一个项目,如果登录是下载一些文件成功,然后打开主 Activity 。如果不是这样,则不会登录也不下载文件并提示用户成功登录下载所需的文件。
  • 失败后,如果用户写入正确的用户名和密码,则它应该下载文件并打开主 Activity 。然而,在在这种情况下,我的应用程序崩溃了。如果用户正确填写了用户名和第一次尝试密码,应用运行顺利。

  • 我有 3 个不同的 LiveData 对象,这样我就可以在 MediatorLiveData 的帮助下观察所有这些对象,并根据条件调用下一个对象。错误输出:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.cesar.sertificar, PID: 7482 java.lang.IllegalArgumentException: This source was already added with the different observer at android.arch.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:89) at com.cesar.sertificar.ui.activity.login.LoginViewModel.handleFirstRunProcess(LoginViewModel.java:101) at com.cesar.sertificar.ui.activity.login.LoginActivity.doLogin(LoginActivity.java:68) at com.cesar.sertificar.ui.activity.login.LoginActivity.onClick(LoginActivity.java:88) at android.view.View.performClick(View.java:5214) at android.view.View$PerformClick.run(View.java:20978) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6134) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

LoginViewModel.class

void handleFirstRunProcess() {
mIsNetworkAvailable.setValue(isAnActiveConnection());
mProcessResult.addSource(mIsNetworkAvailable, isNetworkAvailable -> {
mProcessResult.removeSource(mIsNetworkAvailable);
if (isNetworkAvailable == null) return;

if (!isNetworkAvailable) {
mProcessResult.setValue(new NetworkState(NetworkState.Status.FAILED,
getApplication().getString(R.string.first_run_network_warning)));
return;

}

doLogin(); //First doLogin
});

mProcessResult.addSource(mIsLoginSuccessful, isLoginSuccessful -> {
mProcessResult.removeSource(mIsLoginSuccessful);
if (isLoginSuccessful == null) return;

if (isLoginSuccessful.getStatus() == NetworkState.Status.FAILED) {
mProcessResult.setValue(new NetworkState(NetworkState.Status.FAILED,
getApplication().getString(R.string.login_error)));
return;
}

if (preferenceUtil.getBooleanData(Constants.FIRST_RUN_KEY, true)) {
downloadEmptyRecipientForm(); //Second download form if first run
} else {
mProcessResult.setValue(NetworkState.LOADED); //Open an activity
}
});

mProcessResult.addSource(mIsFormDownloadingSuccessful, isFormDownloaded -> {
mProcessResult.removeSource(mIsFormDownloadingSuccessful);
if (isFormDownloaded == null) {
return;
}

if (isFormDownloaded.getStatus() == NetworkState.Status.FAILED) {
mProcessResult.setValue(new NetworkState(NetworkState.Status.FAILED,
getApplication().getString(R.string.first_run_empty_form_error)));
return;
}

mProcessResult.setValue(NetworkState.LOADED); //Third open an activity
preferenceUtil.putBooleanData(Constants.FIRST_RUN_KEY, false);
});
}

最佳答案

我在 ViewModel 中遇到了同样的问题。似乎这是 MediatorLiveData 的内部限制。如果它看到添加了相同类型的 LiveData,它希望有相同的监听器来使用它。我将添加我的案例,因为它具有相同类型的错误以及之后的情况(没有错误,工作正常)。错误代码:

networkSum = new MediatorLiveData<>();
networkSum.addSource(firstData, integer -> {
if (integer == null) {
firstIsFinished = false;
errorInFirst = false;
} else if (integer == SupportNetworkFunctions.STATUS_IN_PROGRESS) {
networkSum.postValue(integer);
firstIsFinished = false;
} else if (integer == SupportNetworkFunctions.STATUS_FINISHED_WITH_ERROR) {
firstIsFinished = true;
errorInFirst = true;
calculateFinished();
} else {
firstIsFinished = false;
errorInFirst = false;
calculateFinished();
}
});
networkSum.addSource(secondData, integer -> {
if (integer == null) {
secondIsFinished = false;
errorInSecond = false;
} else if (integer == SupportNetworkFunctions.STATUS_IN_PROGRESS) {
networkSum.postValue(integer);
secondIsFinished = false;
} else if (integer == SupportNetworkFunctions.STATUS_FINISHED_WITH_ERROR) {
secondIsFinished = true;
errorInSecond = true;
calculateFinished();
} else {
secondIsFinished = false;
errorInSecond = false;
calculateFinished();
}
});

重构后:

networkSum = new MediatorLiveData<>();
Observer<Integer> statusObserver = new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
if (integer != null) {

if (integer == SupportNetworkFunctions.STATUS_IN_PROGRESS) {
networkSum.postValue(integer);
} else if (integer == SupportNetworkFunctions.STATUS_FINISHED_WITH_ERROR) {
finishedItems++;
errorAppeared = true;
calculateFinished();
} else {
finishedItems++;
calculateFinished();
}
}
}
};
networkSum.addSource(firstData, statusObserver);
networkSum.addSource(secondData, statusObserver);

关于java - MediatorLiveData 多次调用观察者错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52690913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com