Google發(fā)布了新的開源庫Room实牡,他是屬于數(shù)據(jù)庫層的贱傀,可以讓我們順暢的數(shù)據(jù)庫桐臊,更多相關(guān)信息可以移步官方文檔胎撤。
在這篇文章中,我們將使用更加優(yōu)雅的方式來訪問數(shù)據(jù)庫断凶,使用Rxjava來避免在UI線程中操作數(shù)據(jù)庫伤提,從而都移到后臺(tái)線程中操作數(shù)據(jù)。
首先需要在build.gradle文件中引入如下的庫:
// for room
compile "android.arch.persistence.room:runtime:1.0.0-beta1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-beta1"
// for Rxjava
compile "io.reactivex.rxjava2:rxjava:2.0.6"
compile "io.reactivex.rxjava2:rxandroid:2.0.1"
首先創(chuàng)建實(shí)體類User作為示例,最近在學(xué)Kotlin认烁,所以把所有的代碼都改成了Kotlin肿男,如有不正確之處,歡迎指正:
@Entity(tableName = "users")
class User(@ColumnInfo(name = "first_name")
var firstName: String?, @ColumnInfo(name = "last_name")
var lastName: String?) {
@PrimaryKey(autoGenerate = true)
var uid: Int = 0
}
接著我們需要一個(gè)接口UserDao:
@Dao
interface UserDao {
@Query("SELECT * FROM users")
fun getAll(): Maybe<List<User>>
@Query("SELECT * FROM users WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): Flowable<List<User>>
@Query("SELECT * FROM users WHERE first_name LIKE :first AND " + "last_name LIKE :last LIMIT 1")
fun findByName(first: String, last: String): User
@Query("SELECT * FROM users where uid = :id")
fun findById(id: Int): Maybe<User>
@Insert
fun insertAll(vararg users: User)
@Delete
fun delete(user: User)
@Update
fun updateUsers(vararg users: User)
}
接著添加AppDatabase
@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
在我們的示例中却嗡,直接通過調(diào)用LocalCacheManager來訪問數(shù)據(jù)庫
class LocalCacheManager(private val context: Context) {
private val db: AppDatabase
init {
db = Room.databaseBuilder(context, AppDatabase::class.java, DB_NAME).build()
}
val users: Maybe<List<User>>
get() = db.userDao().getAll()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
fun addUser(firstName: String, lastName: String): Completable {
return Completable.fromAction {
val user = User(firstName, lastName)
db.userDao().insertAll(user)
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
fun deleteUser(user: User): Completable {
return Completable.fromAction { db.userDao().delete(user) }.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
fun updateUser(user: User): Completable {
user.firstName = "first name first name"
user.lastName = "last name last name"
return Completable.fromAction { db.userDao().updateUsers(user) }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
}
companion object {
private val DB_NAME = "database-name"
private var _instance: LocalCacheManager? = null
fun getInstance(context: Context): LocalCacheManager {
if (_instance == null) {
_instance = LocalCacheManager(context)
}
return _instance as LocalCacheManager
}
}
}
原示例中使用的是接口回調(diào)的方式來通知數(shù)據(jù)操作完成的舶沛,我把這里都做了一些修改。
這樣窗价,所有的操作都移到了io線程中如庭。
原文地址:https://medium.com/@alahammad/database-with-room-using-rxjava-764ee6124974