Last active
July 25, 2019 19:55
-
-
Save mitchtabian/a5f35fd6e2327fac1a8ab42eb012ca2a to your computer and use it in GitHub Desktop.
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
package com.codingwithmitch.coroutineexamples | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
import kotlinx.coroutines.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button.setOnClickListener { | |
setTextOnMainThread("Click!") | |
fakeApiRequest() | |
} | |
} | |
fun setTextOnMainThread(input: String){ | |
CoroutineScope(Dispatchers.Main) | |
.launch { withContext(Dispatchers.Main){ | |
val newText = text.text.toString() + "\n${input}" | |
text.setText(newText) | |
} } | |
} | |
fun fakeApiRequest(){ | |
CoroutineScope(Dispatchers.IO) | |
.launch{ withContext(Dispatchers.IO){ | |
val result1 = getResult1FromApi() | |
if(result1.equals("Result #1")) { | |
setTextOnMainThread("Got Result #1") | |
val result2 = getResult2FromApi() | |
if(result2.equals("Result #2")){ | |
setTextOnMainThread("Got Result #2") | |
} | |
else{ | |
setTextOnMainThread("Couldn't get Result #2") | |
} | |
} | |
else{ | |
setTextOnMainThread("Couldn't get Result #1") | |
} | |
} } | |
} | |
suspend fun getResult1FromApi(): String{ | |
delay(2000) | |
return "Result #1" | |
} | |
suspend fun getResult2FromApi(): String{ | |
delay(1000) | |
return "Result #2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment