How to build a sequence from an external non-suspending function
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm afraid you're asking for the impossible. Kotlin doesn't implement coroutines through some backdoor JVM magic, but through the transformation of regular Java methods. Suspendable functions involve a hidden method signature change and their return type changes as well. Where your Visitor may have a method void visit(Node)
Object visit(Node, Continuation)
|
When should I make my normal function suspending function?
Date : March 29 2020, 07:55 AM
Hope this helps You want to offload CPU-intensive computation to a background thread so your GUI thread isn't blocked. You don't have to declare any suspending function to achieve that. This is what you need: myActivity.launch {
val processedList = withContext(Default) { processList(list) }
... use processedList, you're on the GUI thread here ...
}
suspend fun processList(list: List<String>): List<Info> = withContext(Default) {
list.map { it.toInfo() }
}
fun String.toInfo(): Info = // whatever it takes
|
How to call Kotlin suspending coroutine function from Java 7
Tag : java , By : alchemist
Date : March 29 2020, 07:55 AM
To fix the issue you can do You have several options depending on your environment. If you are using RxJava2 in the project, the module kotlinx-coroutines-rx2 has utility functions to convert back and forth between coroutines and Rx datatypes. suspend fun sayHello(): String {
delay(1000)
return "Hi there"
}
fun sayHelloSingle(): Single<String> = GlobalScope.rxSingle { sayHello() }
abstract class Continuation<in T> : kotlin.coroutines.Continuation<T> {
abstract fun resume(value: T)
abstract fun resumeWithException(exception: Throwable)
override fun resumeWith(result: Result<T>) = result.fold(::resume, ::resumeWithException)
}
sayHello(new Continuation<String>() {
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resume(String value) {
doSomethingWithResult(value);
}
@Override
public void resumeWithException(@NotNull Throwable throwable) {
doSomethingWithError(throwable);
}
});
|
How to emit from a LiveData builder from a non-suspending callback function
Tag : kotlin , By : user186012
Date : March 29 2020, 07:55 AM
Hope this helps The solution involves wrapping the UrlRequest.Callback traditional callback structure in a suspendCoroutine builder. I also captured my learning in a Medium article which discusses Cronet integration with LiveData and Kotlin Coroutines. override suspend fun getLiveData(): LiveData<List<MyItem>> = liveData(coroutineDispatcher) {
lateinit var result: List<MyItem>
suspendCoroutine<List<MyItem>> { continuation ->
val requestBuilder = cronetEngine.newUrlRequestBuilder(
"http://www.exampleApi.com/example",
object : UrlRequest.Callback() {
// other callbacks not shown
override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
byteBuffer?.flip()
byteBuffer?.let {
val byteArray = ByteArray(it.remaining())
it.get(byteArray)
String(byteArray, Charset.forName("UTF-8"))
}.apply {
val myItems = gson.fromJson(this, MyItem::class.java)
result = myItems
continuation.resume(result)
}
byteBuffer?.clear()
request?.read(byteBuffer)
},
executor
)
val request: UrlRequest = requestBuilder.build()
request.start()
}
emit(result)
}
|
Suspending function can only be called within coroutine body
Date : March 29 2020, 07:55 AM
around this issue A Firestore snapshot listener is effectively an asynchronous callback that runs on another thread that has nothing to do with the coroutine threads managed by Kotlin. That's why you can't call emit() inside an asynchronous callback - the callback is simply not in a coroutine context, so it can't suspend like a coroutine. What you're trying to do requires that you put your call to emit back into a coroutine context using whatever method you see fit (e.g. launch), or perhaps start a callbackFlow that lets you offer objects from other threads.
|