4

ワークマネージャーを使用してデータをローカルデータベースに保存し、同じデータをサーバーに配置しました。

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)
4

1 に答える 1