ios - Retrieving specific Firebase data that is stored in a childByAutoId() Reference (Swift) -
this problem may difficult explain.
here json generated firebase:
{ "users" : { "2w1sse5kizarfq5k8ukjp87d8rk2" : { "plainnote" : { "-kizyvagzqcztsi1l4qi" : { "note" : "finally works", "title" : "whats up" }, "-kizy_m3fmw0m8mgx4am" : { "note" : "aye", "title" : "db" }, "-kizpaesa7-wscojfhvv" : { "note" : "this wont work reason", "title" : "helloo" } }, "email" : "jacobsiegel@gmail.com" }, "xtjy66z8xewteilx6e0moo1jeaz2" : { "email" : "123@gmail.com" }, "rfzv1sauf3rtpu7ycqj0vyaogmd2" : { "plainnote" : { "-kizpfbfhcepu3e8hpeu" : { "note" : "now see own data", "title" : "hello" } }, "email" : "whatsup@gmail.com" } } }
here how generate each plain note in swift:
let title = plainnotetitletextfield.text! let note = bodyofnotetextview.text! let usersid = firauth.auth()?.currentuser?.uid if title != "" || note != "" { let data = [ "title": title, "note": note ] self.usersref.child(usersid!).child("plainnote").childbyautoid().setvalue(data)
after this, populate tableview in realtime firebase realtime database:
override func viewdidappear(animated: bool) { var localnotes = [string]() var localbody = [string]() let usersid = firauth.auth()?.currentuser?.uid usersref.child(usersid!).child("plainnote").observeeventtype(.childadded, withblock: { snapshot in let title = snapshot.value?.objectforkey("title") as! string var x = snapshot.value?.objectforkey("note") as! string if x.characters.count > 20 { x = x[0...20] + "..." } else { x = x + "..." } localnotes.insert(title, atindex: 0) localbody.insert(x, atindex: 0) self.notes = localnotes self.notebody = localbody self.tableview.reloaddata() }) }
my problem: trying access plainnote references when tableview cell tapped, can pull specific note title , body firebase. not sure how without looking @ database every time, because firebase creates unique reference each time new note created.
(p.s.: if think understand, need more details, don't hesitate ask!) thank you.
not sure if have answer yet, here in case (i had same problem). way understand want obtain unique keys generated each time can access data stored underneath single one. generate string array , use extract value of key @ single index can fed data retrieval code. done so:
usersref.child(usersid!).child("plainnote").observeeventtype(.childadded, withblock: { snapshot in var idkeys = [string]() //this hold keys snap in snapshot.children.allobjects { let id = snap as! firdatasnapshot idkeys.append(string(id.key)) }}
now have keys, can select 1 using var selectedkey = idkeys[x].
hope helps
Comments
Post a Comment