joshshin
03/12/2021, 11:50 PMfreeze
on a CompletableDeferred
? I was playing around with multithreading, created a CompletableDeferred<SOME_DATA_CLASS>
on the main thread, froze it, passed it to a background thread , then passed it back to the main thread and called complete(SOME_VALUE)
on it. This seemed to actually work, but it doesn't seem like it should.joshshin
03/12/2021, 11:51 PMactual suspend fun parseFrom(url: String): Pair<RssChannel?, Exception?> {
url.freeze()
val deferred = CompletableDeferred<Pair<RssChannel?, Exception?>>()
deferred.freeze()
val parseInbackground: () -> Unit = {
iOSRssParser(url) { rssChannel, error ->
val doOnMain: () -> Unit = {
rssChannel?.let {
deferred.complete(it to null)
}
error?.let {
deferred.complete(null to it)
}
}
doOnMain.freeze()
dispatch_async(dispatch_get_main_queue(), doOnMain)
}
}
parseInbackground.freeze()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND.convert(), 0.convert()), parseInbackground)
deferred.await()
return deferred.getCompleted()
}
russhwolf
03/13/2021, 12:07 AMjoshshin
03/13/2021, 12:25 AM