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!

2 Answers 2

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")
          }

        })
Sign up to request clarification or add additional context in comments.

6 Comments

Bingo, bango, bongo. It's the snapshot.value not child, duh. Thanks!
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? stackoverflow.com/questions/36238507/…
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
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?
Just general...Do you get the error after the compilation or before on the viewController itself.
|
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)
             }
        }
   })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.