https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Rick

06/05/2020, 11:10 AM
Hi need some help, i got error for the suspend function
transactionAwait
: "suspend function can be called only within coroutine body".
Copy code
callRealmSuspend { realm ->
            realm.transactionAwait {
                realm.delete(RegionRealmObject::class.java)
                realm.delete(ServiceRealmObject::class.java)
            }
        }
and my
callRealmSuspend
function looks like this:
Copy code
suspend fun callRealmSuspend(call: (realm: Realm) -> Unit): Realm? {
    var realm: Realm? = null

    try {
        realm = Realm.getDefaultInstance()
        call(realm)
    }  catch (error: java.lang.Exception) {
        LogHelper.e(messages = *arrayOf("Unhandled error occurred: ${error.localizedMessage}."))
    }

    return realm
}
so how do I change
callRealmSuspend
function to fix the error/ provide a coroutine body? thanks!
t

Tijl

06/05/2020, 11:13 AM
you just need
call: *suspend* (realm: Realm) -> Unit)
r

Rick

06/05/2020, 11:16 AM
thanks a lot @Tijl !
👍 1
s

streetsofboston

06/05/2020, 1:44 PM
Or make the function
inline
, since you call the
call
lambda directly from the
callReamSuspend
function.
👍 2
r

Rick

06/05/2020, 4:57 PM
thank you @streetsofboston! that's a good solution as well
2 Views