Room 2.1 now lets you use Kotlin Coroutines by defining suspending DAO functions. Coroutines are great for performing asynchronous operations. They allow you to write natural, sequential code that operates on expensive resources like the database, without having to explicitly pass tasks, results, and errors between threads. Room’s Coroutine support gives you the benefits of concurrency scoping, lifecycle, and nesting in your database operations.
While developing Coroutines support in Room we ended up encountering some unforeseen problems between the threading models in Coroutines and Android’s SQL API. Read on to find out more about these problems, our solutions, and implementation.