Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support to get documents at a location without listening to data Variation #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Geofirestore/Classes/Geofirestore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down