【发布时间】:2019-06-17 19:30:14
【问题描述】:
我在 Google Cloud Firestore 上有一个数据库,我正在 Kotlin 中制作一个 Android 应用程序来访问它。数据库只有一个集合,但有很多文档。 一次用户只需要大约 5-6 个文档。在线数据库的原因是将来可以轻松添加更多文档。 下载后,下载的文档数据很少会更新。 所以,下载后,我希望他们在没有互联网的情况下访问。
我的问题是,即使文档存储在本地,要访问它们,我也必须设置一个侦听器,因此代码变得异步,这使事情变得非常复杂。所以我想以不同的方式将它们存储在本地,这样我就可以毫不拖延地访问数据 .
这是我从缓存中访问文档的方式:
val db = FirebaseFirestore.getInstance().collection("courses")
db.document("doc_name").get(Source.CACHE).addOnCompleteListener { task ->
if (task.isSuccessful) {
// what to write here
// to store the data locally
// if some other method is used ?
} else {
// documents need to be re-downloaded
}
}
我的问题是存储数据的最佳方式是什么以及如何实施这种最佳方式?鉴于所有文档都具有类似但非常复杂的结构,如下所示。
collection/doc_example/:
|----string
|
|----string
|
|----map
| |----string
| |----int
|
|----int
|
|----array{size=3, type=int}
|
|----string
|
|----map
| |----int
| |----array{size=variable, type=map}
| |----map
| |----string
| |----int
| |----array{size=variable, type=string}
|
|----array{size=variable, type=string}
|
|----array{size=variable, type=string} /* note - this array is optional */
|
|----array{size=variable, type=map}
|----map
|----string
|----string
|----int
|----string
|----string /* note - this one string is optional */
【问题讨论】:
标签: android kotlin google-cloud-firestore android-storage