Suspension functions can be called only within cor...
# multiplatform
d
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
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
currentUser?.logOut() is a suspended function as well, sometimes logOut will happen first.. Any solution for this?
c
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