我使用工作管理器将数据保存到本地数据库,然后将相同的数据放入服务器。
在 localDeleteRequest 类中,我将 id 作为 WorkData 传递并使用该 id 来获取存储在本地数据库中的对象
工作管理器功能:
private fun deleteUserWork(userEntity: UserEntity) {
val workManager: WorkManager = WorkManager.getInstance()
val constraint = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val inputData: Data = mapOf("id" to userEntity.id).toWorkData()
val localDeleteRequest: OneTimeWorkRequest = OneTimeWorkRequestBuilder<TestLocalDeleteWorker>()
.setInputData(inputData)
.build()
val networkDeleteRequest: OneTimeWorkRequest = OneTimeWorkRequestBuilder<TestNetworkDeleteWorker>()
.setConstraints(constraint)
.setInputData(inputData)
.build()
workManager.beginWith(localDeleteRequest)
.then(networkDeleteRequest)
.enqueue()
val status = workManager.getStatusById(networkDeleteRequest.id)
.observe(this, Observer {
if (it != null && it.state.isFinished) {
//DELETE FROM DATABASE COMPLETE
}
})
}
private inner class TestLocalDeleteWorker : Worker() {
override fun doWork(): WorkerResult {
deleteUserLocal()
return WorkerResult.SUCCESS
}
private fun deleteUserLocal() {
listViewModel.getUserById(inputData.getInt("id", -1)).observe(this@UserListFragment, Observer {
listViewModel.deleteUserLocal(it!!)
})
}
}
private inner class TestNetworkDeleteWorker : Worker() {
override fun doWork(): WorkerResult {
deleteUserNetwork()
return WorkerResult.SUCCESS
}
private fun deleteUserNetwork() {
listViewModel.deleteUserServer(inputData.getInt("id", -1))
}
}
查看模型类:
class UserListViewModel(application: Application) : AndroidViewModel(application) {
private val databaseUtil: DatabaseUtil = (application as MyApplication).databaseUtil
private val networkUtil: NetworkUtil = (application as MyApplication).networkUtil
var userList: LiveData<PagedList<UserEntity>> = databaseUtil.getUsersList()
fun fetchDataFromServer() {
networkUtil.getUserListFromServer()
}
fun insertUserLocal(userEntity: UserEntity) {
databaseUtil.insertUser(userEntity)
}
fun insertUserServer(userEntity: UserEntity) {
networkUtil.addUser(userEntity)
}
fun deleteUserLocal(userEntity: UserEntity) {
databaseUtil.deleteUser(userEntity)
}
fun deleteUserServer(id: Int) {
networkUtil.deleteUser(id)
}
fun getUserById(id: Int): LiveData<UserEntity> {
return databaseUtil.getUserById(id)
}
}
DeleteUser 本地函数:
fun deleteUser(userEntity: UserEntity) {
val single: Single<Unit> = Single.fromCallable { appDatabase.getUserDao().deleteUser(userEntity) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
single.subscribe(object : SingleObserver<Unit> {
override fun onSuccess(t: Unit) {
Timber.d("Deletion Successful")
}
override fun onSubscribe(d: Disposable) {
}
override fun onError(e: Throwable) {
Timber.d(e)
}
})
}
删除用户服务器:
fun deleteUser(id: Int) {
retrofit.create(RetrofitServiceApi::class.java)
.deleteUser(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : SingleObserver<String> {
override fun onSuccess(t: String) {
Timber.d(t)
}
override fun onSubscribe(d: Disposable) {
}
override fun onError(e: Throwable) {
Timber.d(e)
}
})
}
我面临以下问题:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:135)
at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:116)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:150)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:310)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:97)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:55)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:253)
at androidx.work.impl.utils.EnqueueRunnable.addToDatabase(EnqueueRunnable.java:93)
at androidx.work.impl.utils.EnqueueRunnable.run(EnqueueRunnable.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)