Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created May 3, 2021 12:10
Show Gist options
  • Save manuelvicnt/efcb7a27bc7de98eac8d14e8a5b7c0e6 to your computer and use it in GitHub Desktop.
Save manuelvicnt/efcb7a27bc7de98eac8d14e8a5b7c0e6 to your computer and use it in GitHub Desktop.
class UserRepository(
private val userLocalDataSource: UserLocalDataSource,
private val externalScope: CoroutineScope
) {
// DO NOT USE shareIn or stateIn in a function like this.
// It creates a new SharedFlow/StateFlow per invocation which is not reused!
fun getUser(): Flow<User> =
userLocalDataSource.getUser()
.shareIn(externalScope, WhileSubscribed())
// DO USE shareIn or stateIn in a property
val user: Flow<User> =
userLocalDataSource.getUser().shareIn(externalScope, WhileSubscribed())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment