如何使用 Proto DataStore 保存对象列表

Gui*_*ira 7 android list object datastore proto

如果我有以下类,如何使用 Proto DataStore 保存它的列表?

data class Tag(
    val id: int,
    val name: String
)
Run Code Online (Sandbox Code Playgroud)

我看到的所有指南都在教如何仅保存单个对象。可以给个清单吗?

Che*_*pta 8

您应该考虑在 Room 中存储内容列表,即使原始数据存储也不是存储复杂内容的正确解决方案,

如果您仍然想要,我建议您将存储的数据限制为 10-15 项

到代码--->

  1. 创建您的 proto 文件,重复用于创建listJava 类型
message Student {
  string id = 1;
  string name = 2;
}

message ClassRoom {
  string teacher = 1;
  repeated Student students  = 2; // repeated => list
}

Run Code Online (Sandbox Code Playgroud)
  1. 在你的原型店里,
dataStore.updateData { store ->
       store.toBuilder()
      .clearStudents() // clear previous list
      .setAllStudents(students)// add the new list
      .build()
}


Run Code Online (Sandbox Code Playgroud)

如果您想要查看我的示例应用程序,请阅读数据/域层 https://github.com/ch8n/Jetpack-compose-thatsMine