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?