Skip to content

Instantly share code, notes, and snippets.

View kapur2511's full-sized avatar

Kshitiz Kapur kapur2511

View GitHub Profile
@ExperimentalCoroutinesApi
suspend fun makeSomeThirdPartySdkCall() {
Log.d("makeThirdPartySdkCall", "STARTING EXECUTION")
var result: String? = "lol"
result = consumeResponseSuspendedWrapper { apiCompletionCallback: APICompletionCallback ->
sdkFunction(apiCompletionCallback)
}
Log.d("makeThirdPartySdkCall", "ENDING EXECUTION $result")
}
suspend fun func1(){
Log.d("SUSPENDED: 1", "STARTING EXECUTION")
val result = coroutineScope {
awaitAll(
async {
func2()
},
async {
func3()
},
@ExperimentalCoroutinesApi
suspend fun makeSomeThirdPartySdkCall() {
Log.d("makeHyperVergeFaceMatch", "STARTING EXECUTION")
var result: String? = "lol"
result = consumeResponseSuspendedWrapper { apiCompletionCallback: APICompletionCallback ->
sdkFunction(apiCompletionCallback)
}
Log.d("makeHyperVergeFaceMatch", "ENDING EXECUTION $result")
}
suspend fun funcA(): Int{
Log.d("SUSPENDED: A", "STARTING EXECUTION")
//suspension point or label
funcB()
//suspension point or label
funcC()
Log.d("SUSPENDED: A", "ENDING EXECUTION")
return 4
}
@Nullable
public final Object func2(@NotNull Continuation var1) {
Object $continuation;
label20: {
if (var1 instanceof <undefinedtype>) {
$continuation = (<undefinedtype>)var1;
if ((((<undefinedtype>)$continuation).label & Integer.MIN_VALUE) != 0) {
((<undefinedtype>)$continuation).label -= Integer.MIN_VALUE;
break label20;
}
suspend fun func2(): Int{
Log.d("SUSPENDED: 2", "STARTING EXECUTION")
delay(3000)
Log.d("SUSPENDED: 2", "ENDING EXECUTION")
return 2
}
private suspend fun launch100000Coroutines(){
val time = measureTimeMillis {
runBlocking {
for(i in 1..10000) {
launch {
delay(10L)
}
}
}
}
private fun launch10000Threads(){
val time = measureTimeMillis {
for(i in 1..10000) {
Thread(Runnable {
Thread.sleep(10)
}).run()
}
}
println("Took $time ms to complete 10000 iterations using threads each taking ~10ms")
}
/**
* [CoroutineScope] tied to this [ViewModel].
* This scope will be canceled when ViewModel will be cleared, i.e [ViewModel.onCleared] is called
*
* This scope is bound to
* [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate]
*/
public val ViewModel.viewModelScope: CoroutineScope
get() {
val scope: CoroutineScope? = this.getTag(JOB_KEY)
/**
* A global [CoroutineScope] not bound to any job.
* Global scope is used to launch top-level coroutines which are operating on the whole application lifetime
* and are not cancelled prematurely.
*
* Active coroutines launched in `GlobalScope` do not keep the process alive. They are like daemon threads.
*
* This is a **delicate** API. It is easy to accidentally create resource or memory leaks when
* `GlobalScope` is used. A coroutine launched in `GlobalScope` is not subject to the principle of structured
* concurrency, so if it hangs or gets delayed due to a problem (e.g. due to a slow network), it will stay working