Entitas, DAO & Database
Definisikan skema lokal dengan Room.
Entitas, DAO & Database adalah pelajaran Jetpack Compose Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Jetpack Compose Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Room Is Your Local SQLite
Room is a friendly layer over SQLite. It lets you save data on the device with plain Kotlin classes instead of raw SQL strings. 💾
Three Core Pieces
Room has exactly three building blocks: an Entity for the table, a DAO for the queries, and a Database that ties them together.
An Entity Is a Table
Annotate a data class with @Entity and Room turns it into a database table, one column per property.
@Entity
data class Note(val text: String)Every Row Needs a Key
Mark one field as the @PrimaryKey so each row is uniquely identified. Use autoGenerate to let Room assign ids for you.
@PrimaryKey(autoGenerate = true)
val id: Int = 0The DAO Holds Your Queries
A @Dao interface declares the operations you want. You write the intent, and Room generates the implementation.
@Dao
interface NoteDao {
// queries go here
}Insert With One Annotation
Add a function and tag it with @Insert. Room writes the SQL that saves your entity to the table.
@Insert
suspend fun insert(note: Note)Read With @Query
Use @Query with real SQL to fetch rows. Room checks the query against your schema at compile time.
@Query("SELECT * FROM Note")
suspend fun getAll(): List<Note>Update and Delete Too
The same pattern covers writes: @Update and @Delete change existing rows by matching their primary key.
@Delete
suspend fun delete(note: Note)The Database Class
Declare an abstract class with @Database, list your entities, and expose each DAO. This is the single entry point.
@Database(entities = [Note::class], version = 1)
abstract class AppDb : RoomDatabase() {
abstract fun noteDao(): NoteDao
}Build the Database Once
Create the instance with Room.databaseBuilder. Building it is expensive, so do it a single time and reuse it.
Room.databaseBuilder(context, AppDb::class.java, "app.db").build()Suspend Keeps the UI Smooth
Disk access is slow, so mark DAO functions suspend. Room then runs them off the main thread and never freezes your screen.
Quick Check
Which annotation marks a Kotlin class as a Room database table?
Recap: The Three Pillars
You defined an Entity table, a DAO of suspend queries, and a Database that wires them up. Next you will make those reads reactive.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Entitas, DAO & Database” gratis?
Ya — teks lengkap “Entitas, DAO & Database” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Jetpack Compose Academy, upgrade ke CoddyKit PRO. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Entitas, DAO & Database”?
Definisikan skema lokal dengan Room. Kamu berlatih Jetpack Compose Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Jetpack Compose Academy?
Tidak diperlukan pengalaman sebelumnya. Jetpack Compose Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.
Berapa lama pelajaran “Entitas, DAO & Database” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Jetpack Compose Academy ini?
Ya. Setiap pelajaran Jetpack Compose Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Entitas, DAO & Database
- Query Reaktif yang Mengembalikan Flow
- Pola Repository
- Strategi Caching Offline-First