Suspension functions can be called only within coroutine body for the code doing a remove and after a logout:
Copy code
suspend 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()
c
Corey
12/19/2022, 8:19 PM
You can move your
logOut
function to happen right after
realm.write {}
. Since
write
is a suspend function it will run first, suspend and then call your
currentUser?.logOut()
d
Daniel
12/19/2022, 9:32 PM
currentUser?.logOut() is a suspended function as well, sometimes logOut will happen first.. Any solution for this?
c
Corey
12/20/2022, 1:19 PM
If your
currentUser?.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