-
Notifications
You must be signed in to change notification settings - Fork 11.6k
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
Comments
I think it should use the suspend fun refresh() {
val networkTasks = networkDataSource.loadTasks()
localDataSource.deleteAll()
val localTasks = withContext(dispatcher) {
networkTasks.toLocal()
}
localDataSource.upsertAll(localTasks) // Fixed this line
} |
I got this problem and I fixed this another way! 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 |
Sure! It sounds like you're referring to an issue or a suggestion related to the official Android Architecture Components samples, specifically around improving the data layer in the context of a codelab. Here's a more detailed approach to your concern about reusing mapped data in the Objective
Steps to Reuse Mapped Data in TaskRepository1. Review Existing Structure
2. Using In-Memory Cache for Mapped DataOne approach to reuse the mapped data is to introduce an in-memory cache within the Example: class TaskRepository(private val taskDao: TaskDao) {
// In-memory cache
private val taskCache = mutableMapOf<String, Task>()
suspend fun getTask(taskId: String): Task? {
// Check the cache first
taskCache[taskId]?.let {
return it
}
// If not in cache, fetch from database and map it
val taskEntity = taskDao.getTask(taskId) ?: return null
val task = taskEntity.toDomainModel()
// Store the mapped data in cache
taskCache[taskId] = task
return task
}
suspend fun updateTask(task: Task) {
// Update database
taskDao.updateTask(task.toEntity())
// Update the cache
taskCache[task.id] = task
}
// Other repository methods...
} 3. Optimize Mapping Logic
Example Extension Functions: fun TaskEntity.toDomainModel(): Task {
return Task(id, name, description, isCompleted)
}
fun Task.toEntity(): TaskEntity {
return TaskEntity(id, name, description, isCompleted)
} Code Changes to Architecture Components Sample (PR #994)Update your repository as follows to incorporate cache-based reuse of mapped data. The following pseudo-code assumes you have a basic understanding of the codebase.
Make sure to update the class TaskRepository(private val taskDao: TaskDao) {
private val taskCache = mutableMapOf<String, Task>()
suspend fun getTask(taskId: String): Task? {
taskCache[taskId]?.let {
return it
}
// Fetch and map
val taskEntity = taskDao.getTaskById(taskId) ?: return null
val task = taskEntity.toDomainModel()
taskCache[taskId] = task
return task
}
// Other methods...
}
Put your mapping functions in a convenient file/module which ensures that the mappings are consistent across usages. // TaskMapper.kt
fun TaskEntity.toDomainModel(): Task {
return Task(id = this.id, name = this.name, description = this.description, isCompleted = this.isCompleted)
}
fun Task.toEntity(): TaskEntity {
return TaskEntity(id = this.id, name = this.name, description = this.description, isCompleted = this.isCompleted)
} ConclusionBy implementing an in-memory cache within the |
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())
The text was updated successfully, but these errors were encountered: