gpt4 book ai didi

android - 房间在访问它存储数据时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:37 25 4
gpt4 key购买 nike

我使用工作管理器将数据保存到我的本地数据库,然后将相同的数据放到服务器。

在 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)
}
}

删除用户本地函数:

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)

最佳答案

这是 Room 中的错误。有关详细信息,请参阅 https://issuetracker.google.com/issues/110100403 .我们将在 WorkManager alpha03 中通过移动到 Room 1.1.1-rc1 来解决这个问题。

关于android - 房间在访问它存储数据时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com