'send Kotlin suspend method as parameter

This is my function in retrofit interface

@POST("/api/Auth/login")
suspend fun patientLogin(@Body loginReq: LoginRequest): Response<BaseResponse<User>>

I need to send this patientLogin(data) methods as a parameter into another method which should be a generic method which will call this method in a IO scope and will return the response with a listener (Interface). The only issue is that I am not sure how to send this method itself as a parameter. Thankyou



Solution 1:[1]

So finally I was able to do what I wanted and I just though that I should also post the answer, that may be helpful to others. Thank you for all the contributors who answered to my question.

fun <T : Any> callBaseApi(call: suspend () -> Response<BaseResponse<T>>, apiCallback: ApiCallback<T>) {
    CoroutineScope(Dispatchers.IO).launch {
       try {
           val response = call.invoke()

           withContext(Dispatchers.Main) {
               if (response.isSuccessful) {
                   apiCallback.onSuccess(
                       response.body()?.data,
                       response.body()?.message,
                       response.body()?.status!!
                   )
               } else {
                   apiCallback.onFailure(response.message(), response.code())
                   Log.e("makeApiCall: ", response.errorBody().toString())
               }
           }
       }catch (e:Exception){
           withContext(Dispatchers.Main) {
               apiCallback.onFailure(e.message, -1)
           }
       }
    }
}

and to call this method

callBaseApi({WebServicesHandler.getWebServices(CacheManager.getCurrentUser()!!.token!!)
            .editPatientPersonalInfo(data)}, apiCallback)

Solution 2:[2]

// One cannot return caculcated value from a coroutine, only the job
fun <T, R> CoroutineScope.functionName(block: suspend (T) -> R): Job = launch(Dispatchers.IO) {
    block() //provide T either as another parameter to this function or if this function belongs to a class you take it from a class' state
}

// One can return a deferred value and wait for the actual R value to be calculated
fun <T, R> CoroutineScope.functionName(block: suspend (T) -> R): Deferred<R> = async(Dispatchers.IO) {
    block()
}

// Will execute on the inherited dispatcher from the coroutine that executes this function
suspend fun <T, R> functionName(block: suspend (T) -> R): R = coroutineScope {
    block()
}

// Always executes on the IO dispatcher
suspend fun <T, R> functionName(block: suspend (T) -> R): R = withContext(Dispatchers.IO) {
    block()
}

Solution 3:[3]

I just had the same issue, and your 'suspend' in the parameter definition helped me.

May someone find some clarity with this example:

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class Worker {

    fun doWork() {
        println("Start work")
        Thread.sleep(1000)
        println("End work")
    }

    suspend fun doWorkSuspend() = withContext(Dispatchers.Default) {
        println("Start work suspended")
        Thread.sleep(1000)
        println("End work suspended")
    }

}

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

class Controller {
    private val scope = CoroutineScope(Dispatchers.Default)
    private val startedJobs = mutableListOf<Job>()

    fun launchWork(perform: () -> Unit) {
        perform()
    }

    fun launchWorkSuspended(perform: suspend() -> Unit) {
        val job = scope.launch {
            perform()
        }
        startedJobs.add(job)
    }

}

fun main(args: Array<String>) {

    val controller = Controller()
    val worker = Worker()

    controller.launchWork { worker.doWork() } // fine
    controller.launchWorkSuspended { worker.doWork() } // fine
    controller.launchWorkSuspended { worker.doWorkSuspend() } // fine

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Talha Akbar
Solution 2 J.Grbo
Solution 3 Gunnar Bernstein