Android Kotlin由代理创建。可从后台线程观察到ViewModel

Android Kotlin由代理创建。可从后台线程观察到ViewModel,android,kotlin,exception,delegates,Android,Kotlin,Exception,Delegates,前提:我已经阅读并尝试了其他帖子中提出的所有解决方案,其他用户也有同样的例外情况 我在应用程序的onCreate中启动了一个线程 open class App : Application() { override fun onCreate() { Thread { ... }.start() } } 在某个时刻,总是在线程内部,我有一个类希望使用'by Delegates.observable'实现一个可观察变量 c

前提:我已经阅读并尝试了其他帖子中提出的所有解决方案,其他用户也有同样的例外情况

我在应用程序的onCreate中启动了一个线程

open class App : Application() {

   override fun onCreate() {

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

在某个时刻,总是在线程内部,我有一个类希望使用'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"
//                }
//            }


    }

}
现在,我需要在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 ->
              .....
           }
   }

}

在读取日志文件的过程中,我在线程的方法中看到,在该方法中,我尝试分配值

myVariable = "result"
变量“by Delegates.observable”的


“只有创建视图层次结构的原始线程才能接触其视图。”

我通过将委托放在couroutineScope(Dispatchers.Main)中解决了这个问题。启动{…} 并将活动中的viewModel代码移动到runOnUiThread{..}方法中

为我服务

  CoroutineScope(Dispatchers.Main).launch {
                            myVariable = "result"
                        }
进入我的活动

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

我通过将委托放在couroutineScope(Dispatchers.Main)中解决了这个问题 并将活动中的viewModel代码移动到runOnUiThread{..}方法中

为我服务

  CoroutineScope(Dispatchers.Main).launch {
                            myVariable = "result"
                        }
进入我的活动

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

什么是
onMenuChanged
?我忘了重命名变量。。。现在我修改了代码您的
onVariableChanged
是否与视图交互?
onVariableThreadChanged()
发生了什么?-onVariableChanged:仅更新对象列表-onVariableThreadChanged():仅调用ViewModel的init函数什么是
onMenuChanged
?我忘了重命名变量。。。现在我修改了代码您的
onVariableChanged
是否与视图交互?在
onVariableThreadChanged()
上发生了什么?-onVariableChanged:仅更新对象列表-onVariableThreadChanged():仅被调用到ViewModel的init函数中