type here
import android.util.Logimport androidx.lifecycle.LiveDataimport androidx.lifecycle.MutableLiveDataimport androidx.lifecycle.ViewModelimport androidx.lifecycle.viewModelScopeimport io.realm.kotlin.UpdatePolicyimport io.realm.kotlin.deleteimport io.realm.kotlin.ext.queryimport io.realm.kotlin.ext.toRealmSetimport io.realm.kotlin.query.RealmResultsimport kotlinx.coroutines.launchclass MainViewModel : ViewModel() { private val realm = RealmApplication.realm private val _usersLiveData = MutableLiveData<List<User>>() val usersLiveData: LiveData<List<User>> = _usersLiveData private val _currentUser = MutableLiveData<User>() val currentUser : LiveData<User> = _currentUser fun addUser( email : String , username : String , password : String , otp : String ) { viewModelScope.launch { realm.write { val user = User().apply { this.username = username this.email = email this.password = password } copyToRealm(user , updatePolicy = UpdatePolicy.ALL) val generateOtp = Otp().apply { this.code = otp this.id = user.email.toString() } copyToRealm(generateOtp , updatePolicy = UpdatePolicy.ALL) } } } fun verifyOtp( enteredOtp : String , email : String , callback : (success : Boolean) -> Unit ) { viewModelScope.launch { try { val otpObject = realm.query<Otp>("id == $0" , email).find().first() if (otpObject.code == enteredOtp) { realm.write { this.delete<Otp>() // Delete the used OTP } callback(true) realm.query<User>("email == $0", email).find().first().also { realm.writeBlocking { findLatest(it)?.isLoggedIn = true } } } else { callback(false) } } catch (exception : Exception) { Log.e("OtpViewModel" , "Error verifying user: ${exception.message}") } } } fun loginUser(email : String, password : String, otp : String, callback : (success : Boolean) -> Unit){ viewModelScope.launch { try { val userObject = realm.query<User>("email == $0 AND password == $1", email, password).find().first() if(userObject.isLoggedIn){ val generateOtp = Otp().apply { this.code = otp this.id = email } realm.write { copyToRealm(generateOtp , updatePolicy = UpdatePolicy.ALL) } realm.query<User>("email == $0", email).find().first().also { realm.writeBlocking { findLatest(it)?.isLoggedIn = true findLatest(it)?.currentUser = true } } callback(true) }else{ callback(false) } }catch (exception : Exception){ callback(false) Log.e("LoginViewModel" , "User Login Failed: ${exception.message}") } } } fun checkUserAlreadyExists(email: String, username: String, callback : (success : Boolean) -> Unit){ viewModelScope.launch { try { realm.query<User>("email == $0 OR username == $1", email, username).find().first() callback(true) }catch (exception : Exception){ callback(false) Log.e("SignupViewModel" , "User not exists: ${exception.message}") } } } fun setCurrentUser(selectedUser: User) { viewModelScope.launch { try { realm.query<User>("email == $0", selectedUser.email).find().first().also { realm.writeBlocking { findLatest(it)?.currentUser = true } Log.e("SignupViewModel" , "User updated: Done") } realm.query<User>("email != $0",selectedUser.email).find().also { realm.writeBlocking{ this.apply { it.forEach { it.currentUser = false } } } Log.e("SignupViewModel2" , "User updated: Done") } }catch (exception : Exception){ Log.e("SignupViewModel" , "User not exists: ${exception.message}") } } } fun getAllUsers() { viewModelScope.launch { val userResults: RealmResults<User> = realm.query<User>().find() val users = userResults.toList() _usersLiveData.postValue(users) } } fun currentUser(){ viewModelScope.launch { try { val currentUserResult = realm.query<User>("currentUser==$0", true).find().first() _currentUser.postValue(currentUserResult) val list = currentUserResult.username Log.e("HomeViewModel" , "Current User is: $list") }catch (exception : Exception){ Log.e("HomeViewModel" , "User not found: ${exception.message}") } } }
}`
Error occurs in setCurrentUser Method...other users is not getting updated
The selected user must become true and other users parameters must be false.But keep on getting this error.I tried all the mentioned details in documentations but nothing seems to work. Please if anyone can solve this it would be of great help
[RLM_ERR_WRONG_TRANSACTION_STATE]: Trying to modify database while in read transaction