Created
May 3, 2021 12:10
-
-
Save manuelvicnt/efcb7a27bc7de98eac8d14e8a5b7c0e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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