ios - Firebase - Getting a reference to values stored under childByAutoID -
i've been following tutorial on making chat app, has separate "rooms", it's 1 central list every user - i'm trying alter each user sees list of own rooms (rooms create, , rooms added friends).
this code handles creating room , adding list:
// create new room @ibaction func createroom(_ sender: any) { if let name = newroomtextfield?.text { let newroomref = roomref.childbyautoid() let roomitem = [ "name": name ] newroomref.setvalue(roomitem) // how add room user's room? let userref: firdatabasereference = firdatabase.database().reference().child("users") } } private func observerooms() { // observe method listen new channels being written firebase roomrefhandle = roomref.observe(.childadded, with: { (snapshot) -> void in let roomdata = snapshot.value as! dictionary<string, anyobject> let id = snapshot.key if let name = roomdata["name"] as! string!, name.characters.count > 0 { self.rooms.append(room(id: id, name: name)) self.tableview.reloaddata() } else { print("error! not decode channel data") } }) }
when room created, i'd add newly created room user's "rooms" node in firebase (which i'll try use create user's list of rooms participate in), each user has unique id created childbyautoid
, i'm not sure how access it. firebase db structure looks like:
{ "rooms" : { "-kgk418wujgjyw27tg1-" : { "messages" : { "-kgk42e1zl3xxus0e0c0" : { "senderid" : "2q4vcku1e7hil84obdzgqcq0ph63", "sendername" : "timothy", "text" : "hi" }, "-kgk45ltf3r439mompve" : { "senderid" : "2q4vcku1e7hil84obdzgqcq0ph63", "sendername" : "timothy", "text" : "testing" } }, "name" : "room one" } }, "users" : { "-kgk3zxgap-irfsbpv8d" : { "gender" : "male", "handle" : "testhandle123", "name" : "timothy", "profilepicture" : "https://graph.facebook.com/*removed privacy*/picture?type=large&return_ssl_resources=1", "rooms" : { "roomname" : "" } } } }
how can add room user created in firebase?
edit:
tried this
let userref: firdatabasereference = firdatabase.database().reference().child("users") let key = userref.key let childupdates = ["/\(key)/rooms/": roomitem] userref.updatechildvalues(childupdates)
and ended making second node under "users", instead of within particular user.
{ "rooms" : { "-kgks0vn65gs2r-cz6k0" : { "name" : "room two" } }, "users" : { "-kgks-x63usomnpdwtdn" : { "gender" : "male", "handle" : "testhandle123", "name" : "timothy", "profilepicture" : "https://graph.facebook.com/*removed privacy*/picture?type=large&return_ssl_resources=1" }, "users" : { "rooms" : { "name" : "room two" } } } }
you should read the documentation, here how can store key :
let key = ref.child("posts").childbyautoid().key
Comments
Post a Comment