Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Have the kotlin invoke() function return a Deferred from Coroutine

The invoke function allows a function to be called directly from the class.

How can I get this to work with a Coroutine Deferred result?

I have tried below but am getting Type mismatch Required: Deferred<List<MyModel>> Found: MyUseCase

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class MyUseCase(
    private val repository: MyRepository,
) {
    suspend operator fun invoke(id: String): Deferred<List<MyModel>> =
        CoroutineScope(IO).async {
            repository.fetchApi(id)
        }
    }
val deferredResult: Deferred<List<MyModel>> = viewModelUseCases.myUseCase

>Solution :

invoke allows an instance to be called as though it’s a function, i.e. with parentheses. The invoke function you’ve written would be called by

val deferredResult: Deferred<List<MyModel>> = viewModelUseCases.myUseCase()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading