This question is often asked and resolved in many ways, but if I come back to the same question, it's because I'm a bit confused. This is my first time to deal with Realm ORM.
This repository class state for the implementation on db storage. Its take instance of Realm and ApiService
class LoginRepositoryImpl(var realm: Realm, var apiService: ApiService){
override fun doLogin(email: String, password: String) {
apiService.loginCheck(email, password)
.flatMap { t: EntityToken ->
// check if user already exist and delete it
val checkIfUserExist = getUserIfExist("email", email)
if (checkIfUserExist != null) {
realm.executeTransactionAsync {
checkIfUserExist.deleteFromRealm()
}
}
// insert user into DB
realm.executeTransactionAsync { bgRealm ->
val user = bgRealm.createObject(UserModel::class.java, UserModel.cachedNextId)
user.email = email
user.logged = true
}
// i want to make another request to server
return@flatMap apiService.pingServer(t.refresh_token)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ uR ->
// make update of user table
realm.executeTransactionAsync {
getUserIfExist("email", email)?.fullname = uR.fullname
}
}
}
//function to check if user instance already exist
private fun getUserIfExist(field: String, email: String): UserModel? {
return realm.where(UserModel::class.java)
.equalTo(field, email)
.findFirstAsync()
}
}
Ps.help me to resolve Realm access from incorrect thread
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)