【问题标题】:Confused about LiveData Observe对 LiveData 观察感到困惑
【发布时间】:2019-02-15 13:27:59
【问题描述】:

我的情况是当用户输入加载片段时,检查LoggedIn,true直接进入MainFragment,false跳转到LoginFramgnet。 这是LoadingFragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Logger.t(LoadingFragment::class.java.simpleName).i("onCreateView")
        val binding = LoadingFragmentBinding.inflate(inflater, container, false)
        subscribeUi()
        return binding.root
    }

    fun subscribeUi(){
        val factory: LoadingViewModelFactory = InjectorUtils.provideLoadingViewModelFactory()
        val viewModel: LoadingViewModel = ViewModelProviders.of(this, factory).get(LoadingViewModel::class.java)
        Logger.t(LoadingFragment::class.java.simpleName).i("viewModel = " + viewModel.toString())
        viewModel.checkLogin()
        viewModel.isToLogin.observe(viewLifecycleOwner, Observer {
            if (it){
                findNavController().navigate(R.id.action_loading_fragment_to_garden_fragment)
            }else{
                Logger.t(LoadingFragment::class.java.simpleName).i("to start login")
                findNavController().navigate(R.id.start_login)
            }
        })

    }

这里是 LoadingViewModel:

class LoadingViewModel(
        private val loadingRepository: LoadingRepository
) : ViewModel() {

    val isToLogin: MediatorLiveData<Boolean> = MediatorLiveData()

    fun checkLogin(){
        isToLogin.addSource(loadingRepository.checkLogin()) {
            isToLogin.value = it
        }
    }
}

这里是 Loadingrepository:

fun checkLogin() : MutableLiveData<Boolean> {
        val data: MutableLiveData<Boolean> = MutableLiveData()
        api.httpGet(SDBUrl.CHECK_LOGIN).enqueue(object : Callback<Map<String, Any>>{
            override fun onFailure(call: Call<Map<String, Any>>, t: Throwable) {
                data.value = false
            }

            override fun onResponse(call: Call<Map<String, Any>>, response: Response<Map<String, Any>>) {
                val result = response.body()
                if (result != null && result.containsKey("success")){
                    val isLogin = result["success"] as Boolean
                    data.value = isLogin
                }else{
                    data.value = false
                }
            }
        })
        return data
    }

登录后,popbackto LoadingFragment,isToLogin 观察立即执行 else,LoginFragment 重新启动。当我调试的时候,在LoginFragment popBackStack上稍等片刻,然后回到Loading Fragment,isToLogin观察执行true。所以我很困惑,我该如何解决这个问题。

【问题讨论】:

    标签: android-livedata android-jetpack android-viewmodel


    【解决方案1】:

    最后,我解决了这个问题。

    class LoadingViewModel(
            private val loadingRepository: LoadingRepository
    ) : ViewModel() {
    
        fun checkLogin(): MediatorLiveData<Boolean> {
            val isToLogin : MediatorLiveData<Boolean> = MediatorLiveData()
            isToLogin.addSource(loadingRepository.checkLogin()) {
                Logger.t(LoadingViewModel::class.java.simpleName).i("it = $it")
                isToLogin.value = it
            }
            return isToLogin
        }
    
    }
    

    然后在LoadingFragment中:

    loadingViewModel.checkLogin().observe(viewLifecycleOwner, Observer {
                Logger.i("isToLogin = $it")
                if (it) {
    
                    findNavController().navigate(R.id.action_loading_fragment_to_garden_fragment)
                } else {
    
                    findNavController().navigate(R.id.start_login)
                }
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-13
      • 2012-07-22
      • 2013-05-13
      • 2020-04-16
      • 2023-03-08
      • 2019-08-04
      • 2019-12-30
      • 2022-01-20
      相关资源
      最近更新 更多