Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active February 12, 2022 06:11
Show Gist options
  • Select an option

  • Save manuelvicnt/ea44d3fe8141da46fcb10ce805efe0a3 to your computer and use it in GitHub Desktop.

Select an option

Save manuelvicnt/ea44d3fe8141da46fcb10ce805efe0a3 to your computer and use it in GitHub Desktop.

Revisions

  1. manuelvicnt revised this gist Nov 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FlowWithLifecycleUsage.kt
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ class LocationActivity : AppCompatActivity() {

    // Listen to multiple flows
    lifecycleScope.launch {
    lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
    // As collect is a suspend function, if you want to collect
    // multiple flows in parallel, you need to do so in
    // different coroutines
  2. manuelvicnt revised this gist Sep 13, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FlowWithLifecycleUsage.kt
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ class LocationActivity : AppCompatActivity() {
    // Listen to one flow in a lifecycle-aware manner using flowWithLifecycle
    lifecycleScope.launch {
    locationProvider.locationFlow()
    .flowWithLifecycle(this, Lifecycle.State.STARTED)
    .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
    .collect {
    // New location! Update the map
    }
  3. manuelvicnt revised this gist Jun 1, 2021. 1 changed file with 23 additions and 5 deletions.
    28 changes: 23 additions & 5 deletions FlowWithLifecycleUsage.kt
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,29 @@ class LocationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    locationProvider.locationFlow()
    .flowWithLifecycle(this, Lifecycle.State.STARTED)
    .onEach {
    // New location! Update the map
    // Listen to one flow in a lifecycle-aware manner using flowWithLifecycle
    lifecycleScope.launch {
    locationProvider.locationFlow()
    .flowWithLifecycle(this, Lifecycle.State.STARTED)
    .collect {
    // New location! Update the map
    }
    }

    // Listen to multiple flows
    lifecycleScope.launch {
    lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
    // As collect is a suspend function, if you want to collect
    // multiple flows in parallel, you need to do so in
    // different coroutines
    launch {
    flow1.collect { /* Do something */ }
    }

    launch {
    flow2.collect { /* Do something */ }
    }
    }
    .launchIn(lifecycleScope)
    }
    }
    }
  4. manuelvicnt created this gist Mar 23, 2021.
    12 changes: 12 additions & 0 deletions FlowWithLifecycleUsage.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    class LocationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    locationProvider.locationFlow()
    .flowWithLifecycle(this, Lifecycle.State.STARTED)
    .onEach {
    // New location! Update the map
    }
    .launchIn(lifecycleScope)
    }
    }