Skip to content

Instantly share code, notes, and snippets.

@lamvann
Created March 15, 2020 15:05
Show Gist options
  • Save lamvann/24793dbab4408f571b7e41c0efcde203 to your computer and use it in GitHub Desktop.
Save lamvann/24793dbab4408f571b7e41c0efcde203 to your computer and use it in GitHub Desktop.
API call using custom dispatcher for coroutines in K/N
private class MainDispatcher: CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_main_queue()) { block.run() } // This line gets highlighted when the error happens
}
}
internal class MainScope: CoroutineScope {
private val dispatcher = MainDispatcher()
private val job = Job()
override val coroutineContext: CoroutineContext
get() = dispatcher + job
}
/*
* This is the function that gets consumed on the Swift side
*/
@UnstableDefault
@ExperimentalStdlibApi
fun getNasaData(predicate: (Item) -> Unit) {
MainScope().launch {
NasaApi().info {
predicate(it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment