2

UPDATE

I have been trying to read through all the docs (of firebase and geofire) and searching on the web and I am having issues trying to grasp how to run a query using geofire and populating a uitableview with the data queried.

I have read about the different implementations of populating a table from this post, but I am having trouble understanding how to create either a .Value query or .ChildAdded query properly with the strings(of keys) that the geofire query generates.

Hopefully someone could help me out with some sample code and we can leave it up to help future visitors with similar issues.

Any help would be appreciated! Thanks in advance!

ORIGINAL QUESTION

I am trying to run a query using Firebase using the Geofire add-on and trying to pull a list of posts based on where the user's location as the center. It does work but the tableview is only being filled with one result(which is the the current or latest post). How do I run a geoquery with the user's location to pull ALL the posts that are around them?

Here is my code for reference:

if(userLocation != nil){

        print("geoQuery ran")
        let geoFire = GeoFire(firebaseRef: Firebase(url: "https://myfirebase.firebaseio.com/PostLocation"))
        let center = CLLocation(latitude: self.userLocation!.latitude, longitude: self.userLocation!.longitude)
        let circleQuery = geoFire.queryAtLocation(center, withRadius: 6)
        circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: {
            (key: String!, location: CLLocation!) in
            print("Key '\(key)' entered the search area and is at location '\(location)'")

            let ref = Firebase(url:"https://myfirebase.firebaseio.com/Posts")
            ref.queryOrderedByChild("objectID").queryEqualToValue(key)
                .observeEventType(.Value, withBlock: { snapshot in

                    var tempItems = [NSDictionary]()

                    for item in snapshot.children {
                        let child = item as! FDataSnapshot
                        let dict = child.value as! NSDictionary
                        tempItems.append(dict)
                    }

                    self.items = tempItems
                    self.tableView.reloadData()
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    print(snapshot.value.objectForKey("text"))
                    print(snapshot.value.objectForKey("user"))
                })

        })

Thanks in advance!

Community
  • 1
  • 1
R S
  • 73
  • 8
  • You are replacing self.items with a new NSDictionary each time a .Value event is received. Why would you expect new items to get appended? – Kato Mar 24 '16 at 16:06
  • @Kato Sorry, I'm not too familiar with dictionaries. Do you know how I would add items without replacing the old dictionary? – R S Mar 25 '16 at 03:54
  • @Kato After reading the docs a little more closely, would .ChildAdded be better to retrieve the list specific list I want rather than using .Value which queries the whole location? – R S Mar 25 '16 at 19:39
  • Did you ever manage to sort this out? I am new to Firebase and am planning to make an app where posts will be filtered by location :) – dwinnbrown May 10 '16 at 08:45
  • To my knowledge, I do not think what I was trying to do was capable in firebase without adding my own custom implementation(at that time). I tried to find solutions for a few weeks but ended up ditching firebase as it was taking too much time and labor; ultimately, I ended up using parse. Their geoquery functionality is pretty stellar. – R S May 14 '16 at 02:54
  • really keen to find out. I have the exact same use case. – user2091936 Mar 17 '17 at 12:15

0 Answers0