إعداد SqlDriver للمنصة
توفير برامج تشغيل Android وiOS عبر expect/actual
إعداد SqlDriver للمنصة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What a Driver Does
SQLDelight generates queries, but it needs a SqlDriver to actually talk to SQLite. Each platform ships its own driver.
Why Drivers Differ
Android and iOS open SQLite differently, so there is no single shared driver. This is exactly the job for expect/actual.
Declare the expect Factory
In commonMain you declare an expect function that promises a SqlDriver, without saying how each platform builds it.
expect fun driverFactory(): SqlDriverAdd Platform Driver Deps
Each source set pulls in its own driver artifact: an Android driver in androidMain and a native driver in iosMain.
androidMain: "app.cash.sqldelight:android-driver:2.0.2"
iosMain: "app.cash.sqldelight:native-driver:2.0.2"Android actual Driver
On Android you build an AndroidSqliteDriver, passing the generated schema and a Context to open the file.
actual fun driverFactory(): SqlDriver =
AndroidSqliteDriver(AppDatabase.Schema, context, "app.db")Context on Android
The Android driver needs a Context. Pass the application context in so the database survives the whole app lifecycle.
iOS actual Driver
On iOS you build a NativeSqliteDriver from the same schema. No Context needed; it stores the file in the app sandbox.
actual fun driverFactory(): SqlDriver =
NativeSqliteDriver(AppDatabase.Schema, "app.db")The Schema Object
Both drivers take AppDatabase.Schema. SQLDelight generated it from your .sq file, and it creates the tables on first launch.
Build the Database
With a driver in hand you create the database instance. This AppDatabase is your typed entry point to every query.
val database = AppDatabase(driverFactory())Share One Instance
Creating many databases wastes file handles. Build the driver once and reuse a single AppDatabase across the app.
Inject the Driver
Pass the driver in from each platform rather than hardcoding it. This keeps shared code testable with a fake or in-memory driver.
Quick Check
How does SQLDelight get a platform-specific database driver into shared code?
Recap: Wire the Engine
You declared an expect driver factory, supplied AndroidSqliteDriver and NativeSqliteDriver, and built one AppDatabase. Now you can run queries. ✅
الأسئلة الشائعة
هل درس «إعداد SqlDriver للمنصة» مجاني؟
نعم — نص درس «إعداد SqlDriver للمنصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «إعداد SqlDriver للمنصة»؟
توفير برامج تشغيل Android وiOS عبر expect/actual تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «إعداد SqlDriver للمنصة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إضافة SQLDelight وكتابة مخططات .sq
- إعداد SqlDriver للمنصة
- إدراج الصفوف والاستعلام عنها وتحديثها
- مراقبة الجداول باعتبارها Flow