Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace usage of android.os.AsyncTask with Kotlin Coroutines #2436

Closed
louwers opened this issue May 27, 2024 · 3 comments
Closed

Replace usage of android.os.AsyncTask with Kotlin Coroutines #2436

louwers opened this issue May 27, 2024 · 3 comments
Assignees
Labels
android 💰 bounty S Small Bounty, USD 100

Comments

@louwers
Copy link
Collaborator

louwers commented May 27, 2024

As mentioned by @westnordost in this comment AsyncTask is deprecated and we should replace our usage of it with more modern APIs like Kotlin Coroutines.

A developer could claim a bounty under the modernization bounty direction for this work.

The source files using android.os.AsyncTask should also be converted to Kotlin.

@westnordost
Copy link
Collaborator

westnordost commented May 27, 2024

Five cents from a developer using Kotlin: Migrating to Kotlin coroutines is something that should not be done in isolation only to replace AsyncTasks but in concert with generally changing also the API (or at least internally) to use suspending functions rather than callback-based functions. I am not sure how suspending APIs translate to Java code on the surface, though. Maybe, from the point of view of Java code, suspending functions look like functions with a callback parameter?

With Kotlin's suspending functions, the API to initialize the map could look like this, instead of juggling with callbacks:

suspend fun initMap() {
  val map = mapView.getMap()
  val style = map.setStyle("mapstyle.json")
  style.addImage("someImg", getDrawable(R.drawable.someImg))
  // ...
}

These look like blocking calls, and that's the point, that you can write suspending code like normal code.

In fact, what I did was to write extension functions to wrap the calls for my use into a suspending API to profit of this convenience myself. E.g.

suspend fun MapView.awaitGetMap(): MapLibreMap = suspendCoroutine { cont ->
    getMapAsync { cont.resume(it) }
}

To make a blocking API that has no callback suspending, one would e.g.

suspend fun Style.awaitAddImage(name: String, bitmap: Bitmap) { 
    withContext(IO) { addImage(name, bitmap) }
}

Now, since the native part of MapLibre handles most of the asynchronousness, I guess internally it does make sense to stay with a callback-based approach. So, take my comment with a grain of salt, I reckon it is probably not that helpful / applicable.

@tarkvara
Copy link
Contributor

tarkvara commented Jul 31, 2024

I believe all these AsyncTask calls were removed as part of issue 1607. I do see a couple of AsyncTask calls, but only in Java code, not Kotlin… not sure where that Java code is actually used.

@tarkvara tarkvara self-assigned this Aug 2, 2024
@louwers louwers added 💰 bounty S Small Bounty, USD 100 and removed 💰 bounty labels Aug 2, 2024
@louwers
Copy link
Collaborator Author

louwers commented Aug 19, 2024

This bounty can be paid out, PR is ready but we should hold off merging for a bit because it is a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android 💰 bounty S Small Bounty, USD 100
Projects
Development

No branches or pull requests

3 participants