Skip to content

Instantly share code, notes, and snippets.

View jorgecasariego's full-sized avatar
:bowtie:

Jorge Casariego jorgecasariego

:bowtie:
View GitHub Profile
class Yerba (val name: String, val brand: String, val db: Database){
fun saveYerbaToDb(yerba: Yerba) {
db.save(yerba)
}
}
override fun areItemsTheSame(oldItem: SleepNight, newItem: SleepNight): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun areContentsTheSame(oldItem: SleepNight, newItem: SleepNight): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
class SleepNightDiffCallback : DiffUtil.ItemCallback<SleepNight>() {
}
fun main() {
println("Start")
val activity = Activity()
activity.doLotOfThings()
Thread.sleep(2000)
activity.destroy()
println("Done")
}
class Activity : CoroutineScope by CoroutineScope(Dispatchers.Default) {
fun main() {
println("Start")
val activity = Activity()
activity.doLotOfThings()
Thread.sleep(2000)
activity.destroy()
println("Done")
}
private class Activity {
fun main() = runBlocking {
log("Start")
hello()
log("Done")
}
private suspend fun hello() = coroutineScope {
launch {
// context of the parent, main runBlocking coroutine
println("[${Thread.currentThread().name}] from parent dispatcher")
fun main() = runBlocking {
log("Start")
hello() // not suspendable, no waiting!
log("Done")
}
private fun CoroutineScope.hello() = launch(Dispatchers.Default) {
delay(1000L)
log("Hello1")
fun main() = runBlocking {
println("Start")
hello()
println("Done")
}
suspend fun hello() = coroutineScope {
launch {
delay(1000L)
println("Hello")
fun main() = runBlocking {
println("Start")
hello(this)
println("Done")
}
fun hello(scope: CoroutineScope) { // or as extension function
scope.launch {
delay(1000L)
println("Hello")
fun main() = runBlocking {
println("Start")
// Start a coroutine (Child coroutine of runBlocking)
launch { // or `this.launch`.
delay(1000L)
println("Hello")
}
println("Done")
}