Simple application for Android using kotlin+coroutine+room database

Preface: In general, operations such as creating data cannot be performed in the main thread (UI thread), because the waiting time is long. So the coroutine is to solve this problem.

Step 1: Introduce in module-level build.gradle

    id 'com.android.application'
    // room
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'

 

// Current room 
implementation "androidx.room:room-runtime:2.2.5" 
kapt "androidx.room:room-compiler:2.2.5" // Kotlin uses kapt 
implementation "androidx.room:room-ktx:2.2. 5"//Coroutines support for Room coroutine operation library 

// lifecycle, implementation used with room 
'androidx.lifecycle:lifecycle-extensions:2.2.0' 
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0 '

Well, the preliminary work is ok, let's formally write the room!

Step 2: Create table entities

 Part 3: Write the corresponding Dao interface

 
 

 Step 4: Create database information

 Step 5: Use in activity

 ok, I will add it later if I use it

I am the dividing line ----------------------------------------------- ------------------------------------------------

Guess you like

Origin blog.csdn.net/Yang_Ming_Lei/article/details/132194980
Recommended