Created
March 15, 2020 15:05
-
-
Save lamvann/24793dbab4408f571b7e41c0efcde203 to your computer and use it in GitHub Desktop.
API call using custom dispatcher for coroutines in K/N
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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