ios - Retrieving Data using Firebase Swift -
i'm using firebase swift ios application. found retrieving data tutorial on firebase's guide bit confusing , not sure why when try access existing values in database results in nil values.
this have far:
usersref.queryorderedbychild("fbid").queryequaltovalue(userid).observesingleeventoftype(.value, withblock:{ snapshot in print("snapshot: ",snapshot.value)
here result of printing snapshot.
snapshot: { 1 = { fbid = 1; firstname = michelle; friendlist = { 9 = "kevin c"; }; lastname = c; profilepicurl = "https:; uid = "facebook:1"; }; }
however, line below results in:
fatal error: unexpectedly found nil while unwrapping optional value
firstname = snapshot.value.objectforkey("firstname") as! string
i retrieve values user (firstname, profilepicurl, friendlist, etc) , store them in variables. seems simple perhaps i'm missing something. appreciated.
your fdatasnapshot
not contain child firstname
. contains child 1
.
this because you're performing query , asking value. since query can have many results, returns list of results. when there's 1 result, still list of 1.
the solution loop on children:
usersref.queryorderedbychild("fbid") .queryequaltovalue(userid) .observesingleeventoftype(.value, withblock:{ snapshot in child in snapshot.children { print("loading group \(child.key!)") } })
Comments
Post a Comment