Hi need some help, i got error for the suspend fun...
# coroutines
r
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
you just need
call: *suspend* (realm: Realm) -> Unit)
r
thanks a lot @Tijl !
👍 1
s
Or make the function
inline
, since you call the
call
lambda directly from the
callReamSuspend
function.
👍 2
r
thank you @streetsofboston! that's a good solution as well