https://kotlinlang.org logo
#ktor
Title
# ktor
i

IsaacMart

09/17/2021, 3:52 AM
Hello guys. I have an issue with coroutine and am requesting for a fix in this. My rowToRequest method is a suspend function and throws an error of "Suspension functions can be called only within coroutine body."
Copy code
override suspend fun getAllRequestsById(user_id: Int): List<Request> {
        return DatabaseFactory.dbQuery {
            Requests.select { Requests.rClientId.eq(user_id) or Requests.rTaskerId.eq(user_id) }.mapNotNull { rowToRequest(it) } //  Suspension functions can be called only within coroutine body
        }

    }
Here is my rowToRequest() function
Copy code
private suspend fun rowToRequest(row: ResultRow?): Request? {
        if (row == null) {
            return null
        }
        return userRepository.getUser(row[Requests.user])?.let {user ->
            taskerRepository.getTasker(row[Requests.tasker])?.let { tasker ->
                Request(
                    row[Requests.requestId],
                    row[Requests.rClientId],
                    row[Requests.rTaskerId],
                    user,
                    tasker,
                    row[Requests.requestedSkill],
                    row[Requests.timeStamp],
                    row[Requests.arrivalDate],
                    row[Requests.arrivalTime],
                    row[Requests.taskDescription],
                    row[Requests.task1ImgUrl],
                    row[Requests.task2ImgUrl],
                    row[Requests.task3ImgUrl],
                    row[Requests.task4ImgUrl],
                    row[Requests.notificationStatus],
                    row[Requests.viewHolderNotificationType],
                    row[Requests.receiptNo],
                    row[Requests.taskStatus],
                    row[Requests.isNotificationSeen],
                    row[Requests.dateOfAcceptance],
                    row[Requests.dateOfDecline],
                    row[Requests.dateOfExpire],
                    row[Requests.convenienceFee],
                    row[Requests.paymentDate],
                    row[Requests.paidAmount],
                    row[Requests.paymentOption],
                    row[Requests.paymentStatus],
                    row[Requests.clientRatingStar],
                    row[Requests.clientRatingFeedback],
                    row[Requests.feedbackDate],
                )
            }
        }
    }
a

Aleksei Tirman [JB]

09/17/2021, 7:09 AM
The simplest way to fix this is to wrap the
rowToRequest
call with
runBlocking
inside
mapNotNull
's lambda.
i

IsaacMart

09/17/2021, 8:37 AM
Thank you @Aleksei Tirman [JB].Much appreciated