Skip to content

Commit d845d81

Browse files
committed
Use HandlerThread for Realm dispatcher
Replace the single-thread executor with a HandlerThread-backed CoroutineDispatcher so Realm async queries and change listeners run on a thread with a Looper. Starts a HandlerThread named "RealmQueryThread" and returns Handler(looper).asCoroutineDispatcher(). Removes the unused executor import and obsolete shutdown-hook comment.
1 parent 678143a commit d845d81

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

app/src/main/java/org/ole/planet/myplanet/di/DatabaseModule.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import dagger.hilt.components.SingletonComponent
1111
import javax.inject.Singleton
1212
import kotlinx.coroutines.CoroutineDispatcher
1313
import kotlinx.coroutines.android.asCoroutineDispatcher
14-
import kotlinx.coroutines.asCoroutineDispatcher
1514
import org.ole.planet.myplanet.data.DatabaseService
1615
import org.ole.planet.myplanet.di.RealmDispatcher
1716
import org.ole.planet.myplanet.utils.DispatcherProvider
@@ -30,10 +29,9 @@ object DatabaseModule {
3029
@Singleton
3130
@RealmDispatcher
3231
fun provideRealmDispatcher(): CoroutineDispatcher {
33-
// App-level shutdown hook ownership
34-
return java.util.concurrent.Executors.newSingleThreadExecutor { r ->
35-
Thread(r, "RealmQueryThread")
36-
}.asCoroutineDispatcher()
32+
// Realm async queries and change listeners require a thread with a Looper.
33+
val handlerThread = HandlerThread("RealmQueryThread").also { it.start() }
34+
return Handler(handlerThread.looper).asCoroutineDispatcher()
3735
}
3836

3937
// Realm initialization is handled in DatabaseService

0 commit comments

Comments
 (0)