Skip to content

Flow.collectLatest is error-prone because of no CoroutineScope #3533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dovchinnikov opened this issue Nov 19, 2022 · 1 comment
Open

Flow.collectLatest is error-prone because of no CoroutineScope #3533

dovchinnikov opened this issue Nov 19, 2022 · 1 comment
Labels

Comments

@dovchinnikov
Copy link
Contributor

launch {
  aFlow.collectLatest {
    async { 
      // uses CoroutineScope from launch => not cancelled when collectLatest block is cancelled
    }
  }
}

It should not be possible to use launch/async/etc inside collectLatest {} (e.g. by means of @DslMarker) OR the block should get CoroutineScope receiver.

Easy to forget workaround:

launch {
  aFlow.collectLatest {
    coroutineScope {
      async {
        // ...
      }  
    }
  }
}
@janselv
Copy link

janselv commented Dec 4, 2022

A similar workaround using current collection context

launch {
  aFlow.collectLatest {
    async(currentCoroutineContext()) {  }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants