Hi All, I have written an Android library in Kotlin which calls the C++ library which makes all the network calls. I have build MyLibraryLib as JNI wrapper and exposed them as suspend function and Added another wrapper MyLibraryClient on top of this JNI to expose as normal function. Inside which I am calling these suspend functions in GlobalScope.launch/returnBlocking so that MyLibraryClient can be used by Java code. What I understood is GlobalScope.launch/runBlocking is not supposed to be used for this purpose. My objective is to write a library which will take care of running the CPU or IO operation on the IO thread and return the results back on the main thread so that the Java/Kotlin caller need not worry about the threading and simply call my library function.
e.g. MyLibraryLib looks something like this
suspend fun getProfile(): Response{
returnWithContext(
Dispatchers.IO){
val response = ProfileResponse()
getProfile(response) // It’s an JNI function
return@withContext response
}
}
MyLibraryClient looks something like this
fun getProfile(): Response{
return runBlocking{
val response = async{
MyLibraryLib.getProfile()
}
return@runBlocking response.await()
}
}