Skip to content
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

[Data layer codelab] Section 7 (Create the task repository) should reuse mapped data #994

Open
hecosw opened this issue Feb 13, 2024 · 3 comments

Comments

@hecosw
Copy link

hecosw commented Feb 13, 2024

Here:
suspend fun refresh() { val networkTasks = networkDataSource.loadTasks() localDataSource.deleteAll() val localTasks = withContext(dispatcher) { networkTasks.toLocal() } localDataSource.upsertAll(networkTasks.toLocal()) }

The last line should probably read:
localDataSource.upsertAll(networkTasks.toLocal())

@iencotech
Copy link

I think it should use the localTasks instead of converting the network tasks to local again, otherwise the withContext() has no purpose:

suspend fun refresh() {
    val networkTasks = networkDataSource.loadTasks()
    localDataSource.deleteAll()
    val localTasks = withContext(dispatcher) {
        networkTasks.toLocal()
    }
    localDataSource.upsertAll(localTasks) // Fixed this line
}

@MahdiRahmani80
Copy link

I got this problem and I fixed this another way!
I've gone to FakeTaskDao.kt and added this line tasksStream.emit(tasks)
my code now looks like this:

  override suspend fun upsertAll(tasks: List<LocalTask>) {
    val newTaskIds = tasks.map { it.id }
    _tasks.removeIf { newTaskIds.contains(it.id) }
    _tasks.addAll(tasks)
    tasksStream.emit(tasks) // this line
  }

I think this solution is a little bit better, because the upsertAll function doesn't emit newly updated tasks.

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

No branches or pull requests

4 participants
@iencotech @MahdiRahmani80 @hecosw and others