icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development

Home Page:https://moko.icerock.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CATASTROPHIC ERROR: MvvmFragment -- When Observing with Moko LiveData and using Android Binding Framework

TheArchitect123 opened this issue · comments

I have a MvvmFragment that is subscribing to changes on a MutableLiveData object inside its view model.
There is a Lifecycle observable that is being run after the view is inflated into memory in onViewCreated.

The exception occurs when I try to access the binding object and allocate the value from the view model's live data into the binding view.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.progressReward.addObserver {
            binding.progressBar.progress = it       /// EXCEPTION OCCURS HERE
        }
}
FATAL EXCEPTION: main
                                                                                                    Process: air.au.com.ontherun, PID: 14250
                                                                                                    java.lang.IllegalArgumentException: can't read binding when view not created
                                                                                                    	at dev.icerock.moko.mvvm.MvvmFragment.getBinding(MvvmFragment.kt:21)
                                                                                                    	at dev.icerock.moko.mvvm.livedata.LiveData.addObserver$lambda$0(LiveData.kt:25)
                                                                                                    	at dev.icerock.moko.mvvm.livedata.LiveData.$r8$lambda$ga3cqZuDL7Dlir_4fzsBiyrVfwM(Unknown Source:0)
                                                                                                    	at dev.icerock.moko.mvvm.livedata.LiveData$$ExternalSyntheticLambda0.onChanged(Unknown Source:2)
                                                                                                    	at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
                                                                                                    	at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
                                                                                                    	at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
                                                                                                    	at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
                                                                                                    	at androidx.lifecycle.LiveData$1.run(LiveData.java:93)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    	at android.os.Looper.loop(Looper.java:288)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:7918)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

@Alex009 @YokiToki @darronschall

If anyone can take a look at this ASAP, I'd appreciate it.
This is a pretty serious issue.

I found a temporary workaround for now by wrapping the binding object with a try catch. Then the next time data comes through it's broadcasted again.


  viewModel.progressReward.addObserver {
            try {
                 binding.progressBar.progress = it       
            }
            catch (ex: Exception) {}
        }

Call ld() to get andru livedata and use observe with lifecycle

It works!! Thanks :)