0

how can I get the Value of this GeoFire?

Error Message: Instance member 'query' cannot be used on type 'GeoFire'; did you mean to use a value of this type instead?

 let center = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                
                let circleQuery = GeoFire.query(at: center, withRadius: 20);
                
                circleQuery.observe(.keyEntered) { (key, location,snapshot) in
                              // parse the snapshot for your result.
                               print("Key '\(key)' entered the search area and is at location '\(location)'")
                           }

Original Code: How to query nearest users in Firebase with Swift?

  • Check the original code: `let geofireRef = /* ... */; let geoFire = GeoFire(firebaseRef: geofireRef); geoFire.query(/*...*/)` – Andreas Oetjen Feb 02 '21 at 13:51
  • I made this and now there are not any errors but the App is crashing.... –  Feb 02 '21 at 14:36
  • because of empty array...(Thread 1: "*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray") –  Feb 02 '21 at 14:39
  • I don' see any array in your code; you'll have to provide more to show the exact line where it is crashing. – Andreas Oetjen Feb 02 '21 at 14:41
  • var geofire = GeoFire(firebaseRef: querySnapshot!.documents[0].get("koordinaten") as! DatabaseReference) –  Feb 02 '21 at 14:42
  • Well, `querySnapshot!.documents` seems to be an empty array, so you cannot access element 0 from it. Btw, you should avoid those crashes by using `if let` or at least _optional chaining_ – Andreas Oetjen Feb 02 '21 at 15:24
  • Can you give me an example? If (querySnapshot!.documents == null) ?? –  Feb 02 '21 at 16:46
  • But than it is only skipped or? So the App does not crash but it does not work –  Feb 02 '21 at 16:50

2 Answers2

0

From the question you linked, and the documentation, it seems the method is called

geoFire.queryAtLocation(center, withRadius: 20);

Also note that both locations show that you need to initialize an instance of GeoFire. You can't just call query methods on the class.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • queryAtLocation(_:withRadius:)' has been renamed to 'query(at:withRadius:)' –  Feb 02 '21 at 16:41
0

To summarize the above comments of me:

  • You need to create an instance of GeoFire to access the query methods.
  • To create that instance, you need to connect it to a Firebase database first (firebaseRef needs to point anywhere into the database).

Hence:

let geofireRef = Database.database().reference()
let geoFire = GeoFire(firebaseRef: geofireRef)

Then, to query that instance, simply use it:

let center = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                
let circleQuery = geoFire.query(at: center, withRadius: 20);

and then go on to observe the circleQuery

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • output is: " " –  Feb 03 '21 at 10:59
  • I don't understand you comments. First, _what_ output do you mean? The `print()` will output something like `Key ...`. Then, what is the second device? – Andreas Oetjen Feb 04 '21 at 06:47
  • Ok I want to see all people around my own location. I already implemented the location manager with didUpdate location and wrote it to the firebase database. Now I want to fetch in a new function the users which are in my zone.... –  Feb 05 '21 at 10:31
  • OK, but that's a completly different question. You should accept this and create a new one, and provide more code / sample data. – Andreas Oetjen Feb 05 '21 at 11:00
  • https://stackoverflow.com/questions/66062480/get-all-users-in-your-zone-with-geofire-4-firebase-7-at-swift-5 –  Feb 05 '21 at 11:30
  • There is the new topic with all details I give –  Feb 05 '21 at 11:31