Skip to content

Android Room DAO — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Android Room DAO. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Room DAO queries run on the main thread, return null when you expect data, or throw SQLiteConstraintException on conflicts.

Wrong Approach ❌

@Dao
interface UserDao {
    @Query("SELECT * FROM users WHERE id = :id")
    fun getUser(id: String): User // Blocking! Crashes on main thread

    @Insert
    fun insert(user: User) // No conflict handling

    @Query("DELETE FROM users WHERE name LIKE :pattern")
    fun deleteByName(pattern: String) // SQL injection risk with raw strings
}

Output: IllegalStateException: Cannot access database on the main thread.

Right Approach ✅

@Dao
interface UserDao {
    @Query("SELECT * FROM users WHERE id = :id")
    suspend fun getUser(id: String): User? // Coroutine-safe

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(user: User) // Handles duplicate keys

    @Query("DELETE FROM users WHERE name LIKE :pattern")
    suspend fun deleteByName(pattern: String) // Safe with parameter binding

    @Transaction
    @Query("SELECT * FROM users")
    suspend fun getAllWithOrders(): List<UserWithOrders> // Transactional read
}

Output: Coroutine-safe DAO that runs on background threads with proper conflict handling.

Prevention

  • Always use suspend functions for Room operations in Kotlin.
  • Use onConflict = OnConflictStrategy.REPLACE or IGNORE for inserts.
  • Annotate complex reads with @Transaction to avoid SQLiteException.
  • Return Flow or LiveData for observable queries.

Common Mistakes with room dao

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Can I return a plain List from a DAO query?

Yes, but only inside a Coroutine. Non-suspend queries that return List execute synchronously and crash on the main thread. Use suspend or wrap in ListenableFuture.

### What is the difference between Flow and LiveData in DAO?

Flow is lifecycle-aware when collected via repeatOnLifecycle and works natively with coroutines. LiveData is Android-specific and auto-observes lifecycle. Prefer Flow for new code.

### How do I handle complex joins in Room DAO?

Create a POJO with @Relation annotations and annotate the DAO method with @Transaction. Room handles the underlying queries automatically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro