我们如何指定source选项以从缓存中的集合中获取多个文档。例如,用户未连接到Internet,并且想强制仅从缓存中使用。
iOS中的示例代码
db.collection("cities").whereField("capital", isEqualTo: true)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
在Android Firestore中,sdk可以通过“ .get(Source.CACHE)”来做到这一点
db.collection("cities")
.whereEqualTo("capital", true)
.get(Source.CACHE)
.addOnSuccessListener { documents ->
for (document in documents) {
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}
答案 0 :(得分:0)
尝试快速代码
db.collection("cities").whereField("capital", isEqualTo: true)
.getDocuments(source: .cache) { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}