我有一个用于在数据库 Realm 上执行某些任务的辅助类。正如您所知,我们在使用 realm 时有一些限制,例如:
但我的问题是,在此 CoroutineScope 上的所有任务都在非循环线程上运行,因此我该如何创建运行在单个循环线程上的 ExecutorCoroutineDispatcher。
显然,我不想使用 Dispatchers.Main,因为它应该在我的数据库上执行任务。
- 非循环线程上的 Realm 实例不会自动刷新。
- 仅可以在创建它们的线程上访问 Realm 对象。
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
。但我的问题是,在此 CoroutineScope 上的所有任务都在非循环线程上运行,因此我该如何创建运行在单个循环线程上的 ExecutorCoroutineDispatcher。
显然,我不想使用 Dispatchers.Main,因为它应该在我的数据库上执行任务。
val handlerThread = HandlerThread(name)
,然后启动它,再在它的 looper 上创建 handler,并将其转换为 dispatcher:Handler(handlerThread.looper).asCoroutineDispatcher()
。 - PawelCaused by: java.lang.NullPointerException: Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
- Mahdi Yusefi