Quantcast
Channel: Active questions tagged use-case - Stack Overflow
Viewing all articles
Browse latest Browse all 80

What would you consider the correct way to implement Use Cases/Repositories that return a Flow (Single Source of Truth + remote fetching)

$
0
0

Suppose I have the following UseCase to fetch users as a flow

class FetchUsersFlowUseCase(    private val userRepository: UserRepository) {    operator fun invoke(): Flow<List<User>> {        return userRepository.fetchUsersFlow()    }}

And a Repository with a function that fetches users as a Flow. When that function is called, an attempt to sync the local database (which is the Single Source of Truth) is also executed.

class UserRepositoryImpl(    private val localUserDataSource: LocalUserDatasource,    private val remoteUserDataSource: RemoteUserDatasource,    private val coroutineScope: CoroutineScope) : UserRepository {    override fun fetchUsersFlow(): Flow<List<User>> {        coroutineScope.launch {            val remoteUsersResult = remoteUserDataSource.fetchUsers()            if (remoteUsersResult.isSuccess) {                // Success: update users locally            } else {                // What to do?                 // How to correctly notify who observes that something wrong happened?            }        }        return localUserDataSource.fetchUsersFlow()    }}

But if something unexpected happens (example: no Internet connection), how can I correctly notify who observes the Flow?


Viewing all articles
Browse latest Browse all 80

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>