Android kotlin+協(xié)程+Room數(shù)據(jù)庫的簡單使用
Room
Room是Google為了簡化舊版的SQLite操作專門提供的
1.擁有了SQLite的所有操作功能
2.使用簡單(類似于Retrofit),通過注解的方式實現(xiàn)相關(guān)功能。編譯時自動生成實現(xiàn)類impl
3.LiveData,LifeCycle,Paging天然融合支持
導入
... plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-android-extensions' id 'kotlin-kapt' } dependencies { //room數(shù)據(jù)庫 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 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0' }
User
package com.zhangyu.myroom.data import android.os.Parcelable import androidx.room.Entity import androidx.room.PrimaryKey import kotlinx.android.parcel.Parcelize @Parcelize @Entity(tableName = "User") data class User( @PrimaryKey var id: String, var name: String ) : Parcelable
UserDao
package com.zhangyu.myroom.data import androidx.room.* @Dao interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun putUser(cacheBean: User) @Query("select * from User where id =:id") suspend fun getUser(id: String): User? @Query("select * from User") suspend fun getAllUser(): List<User>? @Delete fun delete(user: User) @Update(onConflict = OnConflictStrategy.REPLACE) fun update(user: User) }
UserDatabase
package com.zhangyu.myroom.data import android.util.Log import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase import androidx.sqlite.db.SupportSQLiteDatabase import com.zhangyu.myroom.App private const val TAG = "CacheDataBase" //后續(xù)的數(shù)據(jù)庫升級是根據(jù)這個version來比較的,exportSchema導出架構(gòu) @Database(entities = [User::class], version = 1, exportSchema = false) abstract class UserDatabase : RoomDatabase() { companion object { var dataBase: UserDatabase init { //如果databaseBuilder改為inMemoryDatabaseBuilder則創(chuàng)建一個內(nèi)存數(shù)據(jù)庫(進程銷毀后,數(shù)據(jù)丟失) dataBase = Room.databaseBuilder(App.context, UserDatabase::class.java, "db_user") //是否允許在主線程進行查詢 .allowMainThreadQueries() //數(shù)據(jù)庫創(chuàng)建和打開后的回調(diào),可以重寫其中的方法 .addCallback(object : Callback() { override fun onCreate(db: SupportSQLiteDatabase) { super.onCreate(db) Log.d(TAG, "onCreate: db_user") } }) //數(shù)據(jù)庫升級異常之后的回滾 .fallbackToDestructiveMigration() .build() } } abstract fun getUserDao(): UserDao }
MainActivity
package com.zhangyu.myroom import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import com.zhangyu.myroom.data.User import com.zhangyu.myroom.data.UserDatabase import kotlinx.coroutines.launch private const val TAG = "MainActivity" class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) testCache() } private fun testCache() { val userDao = UserDatabase.dataBase.getUserDao() userDao.putUser(User("1001", "zhangyu")) userDao.putUser(User("1002", "liming")) lifecycleScope.launch { val users = userDao.getAllUser() Log.e(TAG, "users: $users") val user = userDao.getUser("1001") Log.e(TAG, "user: $user") Log.e(TAG, "testCache: 協(xié)程執(zhí)行完畢") } Log.e(TAG, "testCache: ") } }
結(jié)果
E/MainActivity: testCache:
E/MainActivity: users: [User(id=1001, name=zhangyu), User(id=1002, name=liming)]
E/MainActivity: user: User(id=1001, name=zhangyu)
E/MainActivity: testCache: 協(xié)程執(zhí)行完畢
到此這篇關(guān)于Android kotlin+協(xié)程+Room數(shù)據(jù)庫的簡單使用的文章就介紹到這了,更多相關(guān)Android kotlin協(xié)程使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實現(xiàn)仿美團或淘寶的多級分類菜單效果示例【附demo源碼下載】
這篇文章主要介紹了Android編程實現(xiàn)仿美團或淘寶的多級分類菜單效果,結(jié)合實例形式分析了Android多級菜單的實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01Android實現(xiàn)院系專業(yè)三級聯(lián)動
這篇文章主要為大家詳細介紹了Android實現(xiàn)院系專業(yè)三級聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03使用ListView實現(xiàn)網(wǎng)上訂餐首頁
這篇文章主要為大家詳細介紹了使用ListView實現(xiàn)網(wǎng)上訂餐首頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01Android RecycleView滑動停止后自動吸附效果的實現(xiàn)代碼(滑動定位)
這篇文章主要介紹了Android RecycleView滑動停止后自動吸附效果的實現(xiàn)代碼(滑動定位),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Android?上實現(xiàn)DragonBones換裝功能
這篇文章主要介紹了Android?上實現(xiàn)DragonBones換裝功能,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06如何在原有Android項目中快速集成React Native詳解
創(chuàng)建一個React Native項目并寫一個純的 React Native 應用可以參考官方指南。下面這篇文章主要給大家介紹了關(guān)于如何在原有Android項目中快速集成React Native的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2017-12-12Android自定義網(wǎng)絡(luò)連接工具類HttpUtil
這篇文章主要介紹了Android自定義網(wǎng)絡(luò)連接工具類HttpUtil,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11