Home > Back-end >  why is this error on couritunes accouring?
why is this error on couritunes accouring?

Time:02-16

2022-02-15 09:53:53.459 7750-7750/? E/ple.anotadomin: Unknown bits set in runtime_flags: 0x8000 2022-02-15 09:53:57.317 7750-7789/com.example.anotadomina E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.example.anotadomina, PID: 7750

Hello there, I'm getting this error while trying to call a fun inside a couritunescope or even global scope, i don't know why, i'm currently on the third day trying to solve the error with google with no luck.

https://github.com/Carlosktdev/Projects

java.lang.IllegalStateException: Method addObserver must be called on the main thread
        at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)
        at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:49)
        at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
        at com.example.anotadomina.views.MainActivity.getMainViewModel(MainActivity.kt:18)
        at com.example.anotadomina.views.MainActivity.access$getMainViewModel(MainActivity.kt:14)
        at com.example.anotadomina.views.MainActivity$add$1.invokeSuspend(MainActivity.kt:36)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

CodePudding user response:

You're indirectly trying to call addObserver on a background thread, while you were supposed to do it on the main thread.

This is due to accessing your viewModel directly from the background coroutine here (via a viewModels() delegate):

    fun add(){
        CoroutineScope(Dispatchers.IO).launch {
            // mainViewModel property access via delegate
            mainViewModel.addScore(ScoreModel(0,15,0))
        }
    }

One solution is to get it from outside the launch, but you should probably just use the lifecycleScope instead of Dispatchers.IO:

    fun add(){
        lifecycleScope.launch {
            // mainViewModel property access via delegate
            mainViewModel.addScore(ScoreModel(0,15,0))
        }
    }

The above requires the lifecycle:lifecycle-runtime-ktx library, which you can add to your project by adding the dependency in your build.gradle.kts:

implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
  • Related