architecture-samples icon indicating copy to clipboard operation
architecture-samples copied to clipboard

[Data layer codelab] Small error in 'refresh()' method in Chapter 7

Open Mario-paul opened this issue 2 years ago • 0 comments

In chapter 7 "Create the Task Repository", and in the 2nd step of section "Save and refresh network data", the code block suggests we do

suspend fun refresh() {
    val networkTasks = networkDataSource.loadTasks()
    localDataSource.deleteAll()
    val localTasks = withContext(dispatcher) {
        networkTasks.toLocal()
    }
    localDataSource.upsertAll(networkTasks.toLocal()) // THIS IS THE ERROR
}

as you can see, localTasks is pointless here because you are doing the same thing in the erroneous line but you are also blocking the main thread. Instead, it should look like this

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

Mario-paul avatar Aug 18 '23 03:08 Mario-paul