前言:一般主線程(UI線程)中是不能執(zhí)行創(chuàng)建數(shù)據(jù)這些操作的,因為等待時間長。所以協(xié)程就是為了解決這個問題出現(xiàn)。
第一步:在模塊級的build.gradle中引入
id 'com.android.application'
// room
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
?
// 現(xiàn)在的room
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5" // Kotlin 使用 kapt
implementation "androidx.room:room-ktx:2.2.5"http://Coroutines support for Room 協(xié)程操作庫
// lifecycle,與room一起使用的
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
好了前期工作ok,正式編寫room吧!
第二步:創(chuàng)建表實體
?第三部:編寫對應(yīng)的Dao接口
?第四步:創(chuàng)建數(shù)據(jù)庫信息
?第五步:在activity中使用
?ok,后續(xù)有使用再添加文章來源:http://www.zghlxwxcb.cn/news/detail-636714.html
我是分界線----------------------------------------------------------------------------------------------文章來源地址http://www.zghlxwxcb.cn/news/detail-636714.html
到了這里,關(guān)于Android使用kotlin+協(xié)程+room數(shù)據(jù)庫的簡單應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!