ios - Firebase Swift: queryEqualToValue by childKey is not working -
has have luck using function:
.queryequaltovalue(value: anyobject?, childkey: string?)
two things:
1) childkey not seem allow deep paths, direct children
2) can't work @ all! given structure:
"post": { "groupname": "hello world" }
if simple:
postref.queryequaltovalue("hello world", childkey: "groupname").observesingleevent( ... )
it not return posts @ all. instead have roundabout way of:
postref.queryorderedbychild("groupname").queryequaltovalue("hello world").observesingleevent( ... )
to work!
am using above function incorrectly?
xcode 7.2.1
swift 2.1.1
ios 9.2
osx 10.10.5
2) can't work @ all! given structure:
"post": { "groupname": "hello world" }
if simple:
postref.queryequaltovalue("hello world", childkey: "groupname") postref.queryequaltovalue("hello world", childkey: "groupname").observesingleevent( ... )
i can query following code:
import uikit import firebase class viewcontroller: uiviewcontroller { var dbref: firdatabasereference! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. dbref = firdatabase.database().reference() dbref.child("post").child("groupname").setvalue("hello world") let postref = dbref.child("post") print(postref) let query = postref.queryequaltovalue("hello world", childkey: "groupname") print(query) --output:-- (/post { en = groupname; ep = "hello world"; sn = groupname; sp = "hello world"; })
1) childkey not seem allow deep paths, direct children
i'm not seeing that. if change code to:
import uikit import firebase class viewcontroller: uiviewcontroller { var dbref: firdatabasereference! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. dbref = firdatabase.database().reference() dbref.child("post").child("groupname").child("userid").setvalue("hello world") let postref = dbref.child("post") print(postref) let query = postref.queryequaltovalue("hello world", childkey: "userid") print(query)
i following output:
(/post { en = userid; ep = "hello world"; sn = userid; sp = "hello world"; })
however, cannot observers work--they don't work docs do.
i delete data on firebase before run app.
edit: okay, think know what's going on observers. according retrieve data section:
firebase data retrieved attaching asynchronous listener
firdatabase referencefirdatabasereference. listener triggered once initial state of data , again anytime data changes.
when try observing query, snapshot in callback doesn't have data in it. on other hand, when observe firdatabasereference
observer works (somewhat) expect.
for example, if observe firdatabasereference this:
import uikit import firebase class viewcontroller: uiviewcontroller { var dbref: firdatabasereference! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. dbref = firdatabase.database().reference() dbref.child("post").child("groupname").child("userid").setvalue("hello world") let postref = dbref.child("post") postref.observeeventtype(.value, withblock: { (snapshot) -> void in print(snapshot) }) sleep(1) //allow observer (in thread) execute before following line: dbref.child("post").child("groupname").child("userid").setvalue("goodbye mars") }
then snapshot in callback has data in it, , output:
snap (post) { groupname = { userid = "hello world"; }; } snap (post) { groupname = { userid = "goodbye mars"; }; }
but if observe firdatabasequery:
let postref = dbref.child("post") let query = postref.queryequaltovalue("hello world", childkey: "userid") query.observeeventtype(.value, withblock: { (snapshot) -> void in print(snapshot) })
then output is:
snap (post) <null>