From 3ba2b832c1b6464017d3e124973bc065d6fb8234 Mon Sep 17 00:00:00 2001 From: Sagar Choudhary Date: Fri, 5 Jun 2020 09:46:07 +0530 Subject: [PATCH 1/2] Add: new nethod to fetch documents for a geoQuery --- Geofirestore/Classes/Geofirestore.swift | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Geofirestore/Classes/Geofirestore.swift b/Geofirestore/Classes/Geofirestore.swift index 9970aa9..d5d5e51 100755 --- a/Geofirestore/Classes/Geofirestore.swift +++ b/Geofirestore/Classes/Geofirestore.swift @@ -197,6 +197,7 @@ public enum GFSEventType { public typealias GFSQueryResultBlock = (String?, CLLocation?) -> Void public typealias GFSReadyBlock = () -> Void public typealias GFSQueryHandle = UInt +public typealias GFSQuerySnapshotsBlock = ([QueryDocumentSnapshot], Error?) -> Void internal class GFSGeoHashQueryListener { var childAddedListener: ListenerRegistration? @@ -593,6 +594,36 @@ public class GFSQuery { return firebaseHandle } + + /** + * Return Array of Document Snapshots matching the Query + */ + public func getAtLocation(completionHandler: @escaping GFSQuerySnapshotsBlock) { + let requestGroup = DispatchGroup() + let newQueries = queriesForCurrentCriteria() + var queryErr : Error? + var result = [QueryDocumentSnapshot]() + for (_, element: query) in newQueries.enumerated() { + requestGroup.enter() + if let query = query as? GFGeoHashQuery{ + let queryFirestore: Query = self.fireStoreQueryForGeoHashQuery(query: query) + queryFirestore.getDocuments() { + snapshot, err in + if let err = err { + queryErr = err + requestGroup.leave() + return + } + result.append(contentsOf: snapshot!.documents) + requestGroup.leave() + } + } + } + + requestGroup.notify(queue: geoFirestore.callbackQueue) { + completionHandler(result, queryErr) + } + } /** * Adds an observer that is called once all initial GeoFirestore data has been loaded and the relevant events have From 27fe07f3e0c597246568f8d6982e4b1ad6b8ce57 Mon Sep 17 00:00:00 2001 From: Sagar Choudhary Date: Fri, 5 Jun 2020 10:53:14 +0530 Subject: [PATCH 2/2] Add: getAtLocation function description to README --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 5eb003c..5c3bc9c 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,22 @@ When the query criteria is updated, the existing locations are re-queried and th ready event is fired again once all events for the updated query have been fired. This includes document exited events for documents that no longer match the query. +#### Query the location "one-shot" +Sometimes it's useful to have the possibility to search for all the documents present in a geographical area without, however, listening to data variations; to do so simply call: + +````swift +query.getAtLocation() { + documentSnapshots, err in + if let err = err { + // Handle error here + } else { + // access documentSnapshots here + documentSnapshots.map{print($0.documentID)} + } +} +```` +It returns a list of all the documents presents in the area and an error if something goes wrong. + #### Updating the query criteria To update the query criteria you can use the `center` and `radius` properties on