1

This may be a simple answer, so apologies in advance, but I'm stuck because I'm still getting head wrapped around how Firebase works. I want to query a Firebase Database based on unix date data that is saved there and then take the related "Unique ID" data and put it into an array.

The data in Firebase looks like this:

posts
  node_0
    Unix Date: Int
    Unique ID Event Number: Int
  node_1
    Unix Date: Int
    Unique ID Event Number: Int
  node_2
    Unix Date: Int
    Unique ID Event Number: Int

What I have so far is as follows. The query part seems to be working as expected. Where I'm struggling is how to put the "Unique ID Event Number" data into an array. This is the approach that seemed closest to success, which is based on this post, but I get an error that child has no member of "value".

// Log user in
if let user = FIRAuth.auth()?.currentUser {

    // values for vars sevenDaysAgo and oneDayAgo set here

    ...

    let uid = user.uid

    //Query Database to get the places searched by the user between one and seven days ago.
    let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")

    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("error")
        } else {
            for child in snapshot.children {
                if let uniqueID = child.value["Unique ID Event Number"] as? Int {
                    arrayOfUserSearchHistoryIDs.append(uniqueID)
                }
            }
        }
    })

} else {
    print("auth error")
}

Any ideas are greatly appreciated!

maxwell
  • 3,788
  • 6
  • 26
  • 40
Ben
  • 3,346
  • 6
  • 32
  • 51

2 Answers2

4

Try using this:-

   historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

       if let snapDict = snapshot.value as? [String:AnyObject]{

               for each in snapDict{

                       let unID = each.value["Unique ID Event Number"] as! Int   
                       arrayOfUserSearchHistoryIDs.append(unID)  
                    }

              }else{
               print("SnapDict is null")
          }

        })
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Bingo, bango, bongo. It's the snapshot.value not child, duh. Thanks! – Ben Oct 14 '16 at 21:44
  • Actually, I spoke too soon. It's not working because I get the error of "ambiguous use of subscript" for the "let unID = " line. I tried applying the ideas from this post, but haven't been successful. Any ideas? http://stackoverflow.com/questions/36238507/ambiguous-use-of-subscript-error-after-new-swift-update – Ben Oct 17 '16 at 15:10
  • Are you coding in swift 2 or swift 3? For swift 2 use each.1[Unique ID Event Number] , also may i suggest dont use spacing in your DB names, causes errors. Change it to **Unique_ID_Event_Number** – Dravidian Oct 17 '16 at 15:19
  • Swift 3. Good point about the spaces. I will transition away from that. Do you think that's what causing the error here or are you just commenting on that in general? – Ben Oct 17 '16 at 15:39
  • Just general...Do you get the error after the compilation or before on the viewController itself. – Dravidian Oct 17 '16 at 15:40
  • View Controller itself. Here's the part that makes me scratch my head...the ambiguous subscript error only started showing up when I import GameplayKit into the class because I need to randomize the array. – Ben Oct 17 '16 at 16:11
2

I ended up re-working how I read the Firebase data based on the approach outlined in this post. The actual working code I used follows in case it's helpful for someone else.

// Log user in
if let user = FIRAuth.auth()?.currentUser {

   let uid = user.uid
   // values for vars sevenDaysAgo and oneDayAgo set here

   ...

   let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")
    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("user data not found")
        }
        else {

             for child in snapshot.children {

                let data = child as! FIRDataSnapshot
                let value = data.value! as! [String:Any]
                self.arrayOfUserSearchHistoryIDs.append(value["Unique ID Event Number"] as! Int)
             }
        }
   })
}
Community
  • 1
  • 1
Ben
  • 3,346
  • 6
  • 32
  • 51