0Pricing
Jetpack Compose Academy · درس

الكيانات وDAOs وقاعدة البيانات

حدّد مخططك المحلي باستخدام Room

الكيانات وDAOs وقاعدة البيانات درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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 = 0

The 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.

الأسئلة الشائعة

هل درس «الكيانات وDAOs وقاعدة البيانات» مجاني؟

نعم — نص درس «الكيانات وDAOs وقاعدة البيانات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

ماذا ستتعلم في «الكيانات وDAOs وقاعدة البيانات»؟

حدّد مخططك المحلي باستخدام Room تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟

لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «الكيانات وDAOs وقاعدة البيانات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟

نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الكيانات وDAOs وقاعدة البيانات
  2. الاستعلامات التفاعلية التي تعيد Flow
  3. نمط المستودع
  4. استراتيجية التخزين المؤقت دون اتصال أولًا
← العودة إلى Jetpack Compose Academy