簡體   English   中英

多個調用的MediatorLiveData觀察器錯誤

[英]MediatorLiveData observer error for multiple calls

  • 如果登錄成功,我將在一個項目中進行下載,然后打開Main Activity。 如果不是這樣,它將既不登錄也不下載文件,並提示用戶成功登錄以下載所需的文件。
  • 失敗后,如果用戶輸入正確的用戶名和密碼,則它應該下載文件並打開“主要活動”。 但是,在這種情況下,我的應用程序崩潰了。 如果用戶在初次嘗試時正確輸入了用戶名和密碼,則該應用程序將順利運行。

  • 我有3個不同的LiveData對象,因此我可以在MediatorLiveData的幫助下觀察所有對象,並根據情況調用下一個對象。 錯誤輸出:

E / AndroidRuntime:致命例外:主進程:com.cesar.sertificar,PID:7482 java.lang.IllegalArgumentException:此源已經與其他觀察者一起添加在android.arch.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:89 )的com.cesar.sertificar.ui.activity.login.LoginActivity.doLogin(LoginActivity.java:68)的com.cesar.sertificar.ui.activity.login.LoginViewModel.handleFirstRunProcess(LoginViewModel.java:101)。 cesar.sertificar.ui.activity.login.LoginActivity.onClick(LoginActivity.java:88)在android.view.View.performClick(View.java:5214)在android.view.View $ PerformClick.run(View.java: 20978),位於android.os.Handler.disleatchMessage(Handler.java:95),位於android.os.Handler.dispatchMessage(Handler.java:95),位於android.os.Looper.loop(Looper.java:145)。位於com.android.internal的java.lang.reflect.Method.invoke(Method.java:372)處的java.lang.reflect.Method.invoke(Native Method)處的app.ActivityThread.main(ActivityThread.java:6134) os.ZygoteInit $ MethodAnd com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)上的ArgsCaller.run(ZygoteInit.java:1399)

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM