Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

587
Views
Is it possible for a function with a Kotlin coroutine to wait for completion and return to a Java class?

I am working on a Java-based Android app. I want to start using Kotlin & coroutines for network-related work off the main thread, but I have run into an issue trying to figure out how to successfully handle a certain situation.

I have created a Kotlin file with a networking function which I plan to call in a Java class (a view model, which at this time needs to stay written in Java). The idea is that I want the networking function to modify and return a list back to the view model, and then allow the view model to proceed.

fun performNetworking(
    data: MutableList<Data>
): MutableList<Data> {
    CoroutineScope(Dispatchers.IO).launch {
        data.forEach {
            // modify each element in the list
        }
    }
    return data
}

Currently, I am calling it from the Java view model as such:

    public void modifyData(List<Data> data) {
        List<Data> modifiedData = NetworkingKt.performNetworking(data);
        performMoreThings(modifiedData);
    }

With this implementation, performMoreThings() triggers as if none of the Data in the list was modified. Through debugging, I discovered that before the coroutine scope can do anything, the conclusion of performMoreThings() is reached with the unmodified Data list.

Therefore, I am wondering if there is a way to have the code wait for the coroutine scope's work to complete before returning the Data list to the Java view model (without locking up the primary thread and preventing the user from interacting with the app)? Or am I misunderstanding the abilities, purposes, and implementations of coroutines and this is not possible? Would I have to pass in a reference to the view model and have it call a continuation method once the coroutine is complete, much as I would do with AsyncTask's in its onPostExecute()?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No, this is not possible. Not because of limitations of coroutines, but the opposite - due to limitations of non-coroutine code. Your modifyData() is a regular, not-suspendable method, so it can't anyhow wait without blocking the thread. Suspend functions of Kotlin can do this, but Java methods can't.

You need to either invoke modifyData() from the background thread, so blocking won't be a problem or redesign performNetworking() to use one of classic techniques for dealing with asynchronous tasks, i.e. receive a callback or return a future:

fun performNetworking(
    data: MutableList<Data>
): CompletableFuture<MutableList<Data>> {
    return CoroutineScope(Dispatchers.IO).future {
        data.forEach {
            // modify each element in the list
        }
        data
    }
}
public void modifyData(List<Data> data) {
    NetworkingKt.performNetworking(data)
            .thenAccept(this::performMoreThings);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!