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 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