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
viewModelScope.launch { | |
// Time passing before we start collecting... | |
delay(4000) | |
// Start collecting the flow | |
sharedFlow.collect { | |
println(it) | |
} | |
} |
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
val sharedFlow = MutableSharedFlow<Int>() | |
viewModelScope.launch { | |
var count = 0 | |
// Every 500ms, emit a new number in sequence | |
while (true) { | |
delay(500) | |
sharedFlow.emit(count++) | |
} |
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
@Composable | |
fun FlashingCountingButton() { | |
val scope = rememberCoroutineScope() | |
var buttonColor by remember { mutableStateOf(Color.Red) } | |
var count by remember { mutableStateOf(1) } | |
Column { | |
Button(colors = ButtonDefaults.buttonColors(backgroundColor = buttonColor), | |
onClick = { | |
val job = scope.launch { |
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
val job = scope.launch { | |
while(true) { | |
delay(500) | |
buttonColor = Color(Random.nextInt(0xFF), Random.nextInt(0xFF), Random.nextInt(0xFF), 0xFF) | |
} | |
} | |
scope.launch { | |
delay(2000) | |
job.cancel() |
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
@Composable | |
fun RandomColourButton() { | |
val scope = rememberCoroutineScope() | |
var buttonColor by remember { mutableStateOf(Color.Red) } | |
Column { | |
Button(colors = ButtonDefaults.buttonColors(backgroundColor = buttonColor), | |
onClick = { | |
scope.launch { | |
while(true) { |
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
suspend fun countToAHundredBillion() { | |
var count = 0L | |
while(count < 100_000_000_000) { | |
count++ | |
// Every 10,000 we yield to the coroutine | |
// dispatcher, allowing this loop to be | |
// cancelled if needed. | |
if (count % 10_000 == 0) { |
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
// !!!!! DON'T DO THIS !!!!! | |
suspend fun countToAHundredBillion_unsafe() { | |
var count = 0L | |
// This suspend fun won't be cancelled if the coroutine | |
// that's running it gets cancelled, because it doesn't | |
// ever yield. | |
while(count < 100_000_000_000) { | |
count++ | |
} |
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
suspend fun deleteAllNotes() = withContext(...) { | |
// Create a scope. The suspend function will return when *all* the | |
// scope's child coroutines finish. | |
coroutineScope { | |
launch { remoteDataSource.deleteAllNotes() } | |
launch { localDataSource.deleteAllNotes() } | |
} | |
} |
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
fun flashTheLights() { | |
viewModelScope.launch { | |
// This seems like an unsafe infinite loop, but in fact | |
// it'll shut down when the viewModelScope is cancelled. | |
while(true) { | |
delay(1_000) | |
lightState = !lightState | |
} | |
} | |
} |
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
suspend fun saveNote(note: Note) { | |
withContext(Dispatchers.IO) { | |
notesRemoteDataSource.saveNote(note) | |
} | |
} |
NewerOlder