如果我有以下类(class),如何使用 Proto DataStore 保存它的列表?
data class Tag(
val id: int,
val name: String
)
我看到的所有指南都在教如何只保存一个对象。是否有可能列出它?
最佳答案
您应该考虑在 Room 中存储内容列表,即使 proto-datastore 也不是存储复杂内容的合适解决方案,
如果您仍然想要,我建议您将存储的数据限制为 10-15 项
到代码--->
list
Java 类型 message Student {
string id = 1;
string name = 2;
}
message ClassRoom {
string teacher = 1;
repeated Student students = 2; // repeated => list
}
dataStore.updateData { store ->
store.toBuilder()
.clearStudents() // clear previous list
.setAllStudents(students)// add the new list
.build()
}
如果您想查看我的示例应用程序,请阅读数据/域层https://github.com/ch8n/Jetpack-compose-thatsMine
关于android - 如何使用 Proto DataStore 保存对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64430872/