2017-10-28 23 views
9

我在我的應用程序中使用Android Architecture Components。在My Login Activty中,當登錄失敗時,我顯示一個對話框!實時數據被多次調用?

由於實時數據,該對話框已顯示3次以上。我加了一些日誌&發現livedata被多次調用。

我該如何解決這個問題?

ACTIVITY

mViewModel.authenticate(token, binding.inputPassword.getText().toString()).observe(LoginActivity.this, apiResponse -> { 
    progress.dismiss(); 
    if (apiResponse != null) { 
     if (apiResponse.getError() != null) { 
      Log.e("Login", "Network Failure"); 
     } else { 
      if (apiResponse.getAuthuser().getStatus().equals("VALID")) { 
       PrefUtils.saveUserToPrefs(LoginActivity.this, apiResponse.getAuthuser()); 
       finish(); 
      } else if (apiResponse.getAuthuser().getStatus().equals("INVALID")) { 
       Log.e("LOGIN Issue ", "Showing Dialog" + apiResponse.getAuthuser().getStatus()); 
       loginFailure(); 
      } 
     } 
    } 
}); 

視圖模型

class LoginActivityViewModel extends ViewModel { 

    private final FarmerRepository farmerRepository; 
    private MediatorLiveData<ApiResponse> mApiResponse; 

    LoginActivityViewModel(FarmerRepository repository) { 
     mApiResponse = new MediatorLiveData<>(); 
     farmerRepository = repository; 
    } 

    MediatorLiveData<ApiResponse> authenticate(String encryptedMobile, String pwd) { 
     mApiResponse.addSource(
       farmerRepository.authenticate(encryptedMobile, pwd), 
       apiResponse -> mApiResponse.setValue(apiResponse) 
     ); 
     return mApiResponse; 
    } 
} 

logcat的

11-01 00:13:31.265 24386-24386 E/LOGIN Issue: Showing DialogINVALID 

11-01 00:13:31.312 24386-24386 E/LOGIN Issue: Showing DialogINVALID 
11-01 00:13:37.034 24386-24386 E/LOGIN Issue: Showing DialogINVALID 

11-01 00:13:38.196 24386-24386 E/LOGIN Issue: Showing DialogINVALID 
11-01 00:13:38.234 24386-24386 E/LOGIN Issue: Showing DialogINVALID 
11-01 00:13:38.273 24386-24386 E/LOGIN Issue: Showing DialogINVALID 

UPDATE

使用SingleLiveEvent後。它沒有被觀察到。你能告訴我代碼有什麼問題嗎?

更新視圖模型

class LoginActivityViewModel extends ViewModel { 

    private final FarmerRepository farmerRepository; 
    private MediatorLiveData<ApiResponse> mApiResponse; 
private SingleLiveEvent<ApiResponse> mMsgUpdate; 

    LoginActivityViewModel(FarmerRepository repository) { 
     mApiResponse = new MediatorLiveData<>(); 
     farmerRepository = repository; 
    mMsgUpdate = new SingleLiveEvent<>(); 
    } 

    SingleLiveEvent<ApiResponse> authenticate(String encryptedMobile, String pwd) { 
     mApiResponse.addSource(
       farmerRepository.authenticate(encryptedMobile, pwd), 
       apiResponse -> mMsgUpdate.setValue(apiResponse) 
     ); 
     return mMsgUpdate; 
    } 
} 
+0

這並不值得賞金,因爲這是LiveData的常見行爲。 https://github.com/googlesamples/android-architecture-components/issues/63 –

+0

@EmanuelS你可以檢查更新嗎? – user3467240

+0

添加多個來源不是應該在視圖模型中完成的工作。它應該在存儲庫中處理的工作 –

回答

5

視圖模型的作用是代表視圖的當前狀態。 LiveData增加了觀察狀態變化的能力。您將您的LiveData對象視爲一種將呼叫傳回呼叫進行身份驗證的方式。相反,您的身份驗證方法應該簡單地將憑據作爲參數,決定是否登錄人員,如果是這樣,則更新LiveData ViewModel以反映該人員已登錄,然後觀察員將得到此消息並最有可能解僱此視圖並顯示您想要顯示的身份驗證狀態的其他任何部分(例如LoggedInUsername)。

  • 使一類叫做CurrentAuthenticatedSession什麼的,與例如用戶名字段,並具有爲null開始與
  • 認證調用時,檢查一看,

    在總結

    所以用戶信息

  • ,如果它是當前更新您的CurrentAuthenticatedSession的LiveData實例
  • 有「currentlyLoggedInUser」現場監聽更新此對象
  • 將該控件的文本設置爲字段用戶名的值

這是一種方法。由於登錄屏幕是暫時的,因此狀態更新的觀察者可能被視爲多餘。但這就是ViewModel/LiveData機制的工作原理。