Joey
12/11/2019, 5:10 AMsuspend fun fetchUsers(): List<String> {
val response = client.get<JsonObject> { url("$baseUrl/heartbeat.json") }
val userIds = response.keys.toList()
return userIds
}
Now im trying to implement it in a pure old java project. I enabled the kotlin plugin in this project and created this function:
fun fetchUsersFromKtLib(): List<String> {
val api = HeartbeatApi()
val users = GlobalScope.future { api.fetchUsers() }
return users.get()
}
Now everytime im calling this fetchUsersFromKtLib
function, there is a jank in my UI (a freeze). Im trying to solve this problem for about 3 days now. So far this is the cleanest way i called the function from the kotlin shared library. What can you suggest to avoid any UI freeze? Thanks guys!yshrsmz
12/11/2019, 5:32 AMfun fetchUsersFromKtLib(callback: (users:List<String>) -> Unit) {
val api = HeartbeatApi()
GlobalScope.launch {
val users = api.fetchUsers()
callback(users)
}
}
you need to convert to callback or return as CompletableFuture and consume it asynchronouslyJoey
12/11/2019, 6:35 AMKotlin shared lib
fun fetchUsersFromKtLib(callback: (String) -> Unit) {
GlobalScope.launch {
val response = client.get<JsonObject> { url("$baseUrl/heartbeat.json") }
val listOfUsers = response.keys.iterator()
listOfUsers.forEach {
callback(it)
}
}
}
Kotlin class inside old java project
fun fetchUsers(listFromAndroid: ArrayList<String>) {
val api = HeartbeatApi()
return api.fetchUsersFromKtLib {
listFromAndroid.add(it)
}
}
Big thanks to @yshrsmzmarstran
12/11/2019, 12:24 PMfun fetchUsersFromKtLib(): CompletableFuture<List<String>> {
val api = HeartbeatApi()
return GlobalScope.future {
api.fetchUsers()
}
}
Joey
12/13/2019, 12:36 AMCompletableFuture
@marstranmarstran
12/13/2019, 12:39 AMGlobalScope.future
return a CompletableFuture
?Joey
12/13/2019, 12:42 AMapi.fetchUsers()
should return the list of string right?marstran
12/13/2019, 12:43 AMJoey
12/13/2019, 12:43 AM