[英]Firestore Geohash Query with Live Updating Results in SwiftUI
我正在嘗試在 SwiftUI 中構建一個 iOS 應用程序,用戶可以在其中找到靠近其當前位置的“帖子”。 我有一個名為 Posts 的子集合,帶有 geohash。 有點煩人的是,谷歌的這個庫已無緣無故地存檔https://github.com/firebase/geofire-objc 。 相反,我不得不使用這個庫https://github.com/emilioschepis/swift-geohash 。
我找到當前用戶周圍的所有相鄰 geohashes,然后針對每個以 geohash 開頭並以 geohash + '~' 結尾的 geohash 運行針對 firstore 的查詢。 這是我寫的 function:
// import https://github.com/emilioschepis/swift-geohash
class FirestorePosts: ObservableObject {
@Published var items = [FirestorePost]() // Reference to our Model
func geoPointQuery(tag:String){
do {
let db = Firestore.firestore().collection("tags")
let docRef = db.document(tag).collection("posts")
// users current location is "gcpu"
let neighbors = try Geohash.neighbors(of: "gcpu", includingCenter: true)
let queries = neighbors.map { bound -> Query in
let end = "\(bound)~"
return docRef
.order(by: "geohash")
.start(at: [bound])
.end(at: [end])
}
func getDocumentsCompletion(snapshot: QuerySnapshot?, error: Error?) -> () {
guard let documents = snapshot?.documents else {
print("Unable to fetch snapshot data. \(String(describing: error))")
return
}
self.items += documents.compactMap { queryDocumentSnapshot -> FirestorePost? in
return try? queryDocumentSnapshot.data(as: FirestorePost.self)
}
}
for query in queries {
print("ran geo query")
query.getDocuments(completion: getDocumentsCompletion)
}
}
catch{
print(error.localizedDescription)
}
}
}
到目前為止,查詢有效並按預期返回項目。 但是,當 Firestore 發生變化時,結果不會實時更新。
query.addSnapshotListener
,但它不喜歡“完成:”參數我對此進行了重構,它似乎可以實時工作和更新。 我不需要使用計數器,因為我將文檔附加到self.items
(雖然不確定那是否正確)。
...
for query in queries {
query.addSnapshotListener { (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("No documents")
return
}
self.items += documents.compactMap { queryDocumentSnapshot -> FirestorePost? in
return try? queryDocumentSnapshot.data(as: FirestorePost.self)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.