简体   繁体   English

Android Kotlin 由 Delegates.observable 从后台线程进入 ViewModel

[英]Android Kotlin by Delegates.observable from background Thread into ViewModel

Premises: I have already read and tried all the solutions proposed in the other posts where other users had had the same exception前提:我已经阅读并尝试了其他用户遇到相同异常的其他帖子中提出的所有解决方案

I have a thread that I start inside my app's onCreate我有一个线程,我在我的应用程序的 onCreate 中开始

open class App : Application() {

   override fun onCreate() {

      Thread {
           ...
             }.start()
   }
}

at some point, always inside the thread, I have a class that wants to implement an observable variable using the 'by Delegates.observable'在某些时候,总是在线程内部,我有一个 class 想要使用“by Delegates.observable”实现一个可观察变量

class MyService{

    var myVariable: String by Delegates.observable("default value") { _, oldValue, newValue ->
        onVariableChanged?.invoke(oldValue, newValue)
    }
    var onVariableChanged: ((String, String) -> Unit)? = null

    fun doSomething(){

     myVariable = "result"

     // ----      I also tried the 2 solutions commented below  ----
//
//            val handler = Handler(getMainLooper())
//            handler.post {myVariable = "result"}

//            GlobalScope.launch {
//                withContext(Dispatchers.Main){
//                   myVariable = "result"
//                }
//            }


    }

}

Now I need that in the ViewModel I could be able to observe the variable that should update within the thread现在我需要在 ViewModel 中可以观察到应该在线程中更新的变量

class MyViewModel(application: Application) : AndroidViewModel(application) {

//     ---- I also tried with couroutines  --------
  //    private val viewModelJob = SupervisorJob()
//    private val viewModelScope = CoroutineScope(viewModelJob + Dispatchers.Main)  

 init {

//        viewModelScope.launch {
//            onVariableThreadChanged()
//        }

        onVariableThreadChanged()
      }

   private fun onVariableThreadChanged(){
           myServiceInstance.onVariableChanged = {oldValue , newValue ->
              .....
           }
   }

}

In reading the log file I see that in the method of the thread where I try to assign the value在阅读日志文件时,我看到在我尝试分配值的线程方法中

myVariable = "result"

of the variable 'by Delegates.observable' gives me an exception变量'by Delegates.observable'给了我一个例外

"Only the original thread that created a view hierarchy can touch its views." “只有创建视图层次结构的原始线程才能触及它的视图。”

I solved by putting the delegate inside the couroutineScope (Dispatchers.Main).launch {...} and moving the viewModel code inside the activity into the runOnUiThread {..} method我通过将委托放在 couroutineScope (Dispatchers.Main).launch {...} 中并将 Activity 中的 viewModel 代码移动到 runOnUiThread {..} 方法中来解决

into MyService进入我的服务

  CoroutineScope(Dispatchers.Main).launch {
                            myVariable = "result"
                        }

into MyActivity进入我的活动

  runOnUiThread(
            myServiceInstance.onVariableChanged = {oldValue , newValue  ->
              .....
             }
         )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM