制造业小程序网站开发,php是做网站的吗,商丘seo快速排名,河北网站seo外包文章目录
前言集成Room结合协程的使用总结
一、前言,
现在kotlin 是趋势#xff0c;那必然就要用到协程#xff0c;还有就是随着jetpack 的发力#xff0c;带来了很多好用的库#xff0c;比如今天提到Room#xff0c;是一个类似greenDao的数据库。它不但支持kotlin协程…文章目录
前言集成Room结合协程的使用总结
一、前言,
现在kotlin 是趋势那必然就要用到协程还有就是随着jetpack 的发力带来了很多好用的库比如今天提到Room是一个类似greenDao的数据库。它不但支持kotlin协程/RxJava还具备编译期检查是非常友好的库。我们一起来看下在项目中怎么使用。
二、集成Room
1、创建一个kotlin项目然后在app里面的build.gradle添加依赖
plugins {
...id kotlin-android-extensionsid kotlin-kapt
}
dependencies {
...//room数据库implementation androidx.room:room-runtime:2.4.2kapt androidx.room:room-compiler:2.4.2 // Kotlin 使用 kaptimplementation androidx.room:room-ktx:2.4.2//Coroutines support for Room 协程操作库//lifecycleimplementation androidx.lifecycle:lifecycle-extensions:2.2.0implementation androidx.lifecycle:lifecycle-runtime-ktx:2.2.0}此时我们同步一下开始运行项目。会报错
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8解决将项目的jdk1.8 改成 jdk11。 接下来还有报错
Cant determine type for tag macro namem3_comp_assist_chip_container_shape?attr/shapeAppearanceCornerSmall/macro原因是material 的版本高了的问题。 implementation com.google.android.material:material:1.8.0解决我们将material改成1.6.0。项目就能正常运行了。
2、相关的类的创建
2.1创建数据库的实体类 通过在实体类上加注解Entity,这样实体类就相当于是一张表
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.android.parcel.ParcelizeParcelize
Entity(tableName Student)
data class Student(PrimaryKeyvar id: String,var name: String
) : Parcelable2.2创建实体类的Dao 通过在Dao接口上加注解Dao,就可以让dao轻松地完成增删改查。 另外可以将 suspend Kotlin 关键字添加到 DAO 方法中用 Kotlin 协程功能使这些方法成为异步方法。这样可确保不会在主线程上执行这些方法。
Dao
interface StudentDao {//通过Insert 注解的onConflict 解决冲突如果有老的数据存在则会进行替换,如果没有就插入Insert(onConflict OnConflictStrategy.REPLACE)suspend fun putStudent(cacheBean: Student)Query(select * from Student where id :id)suspend fun getStudent(id: String): Student?Query(select * from Student)suspend fun getAllStudent(): ListStudent?Deletesuspend fun delete(student: Student)Update(onConflict OnConflictStrategy.REPLACE)suspend fun update(student: Student)}2.3 创建数据库的类 创建一个类继承RoomDatabase,加注解Database,轻松地建数据库和建表
import android.util.Log
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase//后续的数据库升级通过这个version来控制exportSchema是否导出数据库的配置信息
Database(entities [Student::class], version 2, exportSchema false)
abstract class StudentDatabase : RoomDatabase() {companion object {var dataBase: StudentDatabaseval TAG StudentDatabase::class.java.simpleNameinit {//如果databaseBuilder改为inMemoryDatabaseBuilder则创建一个内存数据库进程销毁后数据丢失dataBase Room.databaseBuilder(MyApplication.getApplicationContext(), StudentDatabase::class.java, db_user)//数据库的操作是否允许在主线程中执行.allowMainThreadQueries()//数据库创建和打开后的回调可以重写其中的方法.addCallback(object : Callback() {override fun onCreate(db: SupportSQLiteDatabase) {super.onCreate(db)Log.d(TAG, onCreate: db_student)}})//数据库升级异常之后的回滚.fallbackToDestructiveMigration().build()}}abstract fun getStudentDao(): StudentDao
2.4 创建一个MyApplication 通过它我们来获取context。
class MyApplication : Application() {init {instance this}companion object {private var instance: MyApplication? nullfun getApplicationContext() : Context {return instance!!.applicationContext}}override fun onCreate() {super.onCreate()}}三、接下来就是使用了
通过点击按钮进行增删改查的操作。 增加数据 btn_add.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()studentDao.putStudent(Student(101,小李));studentDao.putStudent(Student(102,小王))}}更改数据 btn_update.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val student studentDao.update(Student(101,小陈))Log.d(TAG,student.toString());}}删除数据 btn_delete.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val student studentDao.delete(Student(101,小陈))Log.d(TAG,student.toString());val students studentDao.getAllStudent()Log.d(TAG, students: $students)}}查询数据 lifecycleScope.launch {btn_query.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val students studentDao.getAllStudent()Log.d(TAG, students: $students)}}备注数据库的操作一定要放到子线程中切不可在主线程中操作虽然可以通过allowMainThreadQueries强制开启允许这么做但这个是测试时用实际项目中还是不要在主线程中操作数据库避免遇到ANR问题
源码地址https://github.com/shenshizhong/KotlinRoomDemo
总结
1 、导入依赖, 同步 2 、创建一个实体类Student,加注解Entity就相当于一张表 3 、创建一个接口StudentDao,加注解Dao就可以完成增删改查 4 、创建抽象类继承RoomDatabase,加注解Database轻松地建数据库和建表 5 、通过数据库的实例获取dao调用方法
如果对你有一点点帮助那是值得高兴的事情。) 我的csdnhttp://blog.csdn.net/shenshizhong 我的简书http://www.jianshu.com/u/345daf0211ad