Daniel
12/18/2022, 7:30 PMsuspend fun doLogout(token: String) {
val userId = appService.currentUser?.id
realm.write {
var user = query<UserInfo>("_id = $0", userId).first().find()
if (user != null) {
val productIndex =
user.FCMToken.withIndex().findLast { it.value == token }!!.index
user = findLatest(user)!!.also {
it.FCMToken.removeAt(productIndex)
}
copyToRealm(user)
appService.currentUser?.logOut() // Suspension functions can be called only within coroutine body
}
}
}
Is there anyway to fix this without changing much code?
I need to do first the db operation then logOut()Corey
12/19/2022, 8:19 PMlogOut
function to happen right after realm.write {}
. Since write
is a suspend function it will run first, suspend and then call your currentUser?.logOut()
Daniel
12/19/2022, 9:32 PMCorey
12/20/2022, 1:19 PMcurrentUser?.logOut()
is called after write {}
which is also a suspend function, it is guaranteed that the former will be called only after latter is resumed