ios - updateChildValues: not saving all data -
a user enters post tags. tags come array , i'm supposed check if tag exist or not , use tagid write post database tags relate post (see below). whats happening last tagid being recorded database not of them
for item in tags { ref.child("tags").observesingleeventoftype(.value, withblock: { snapshot in if snapshot.childsnapshotforpath(item.lowercasestring).exists() { let tagkey = snapshot.childsnapshotforpath(item.lowercasestring).value as! string self.savetagid(tagkey, postkey: postkey, uid: userid) } else { let tagkey = self.ref.child("tags").childbyautoid().key let tagupdate = ["/tags/\(item.lowercasestring)": tagkey] self.ref.updatechildvalues(tagupdate) self.savetagid(tagkey, postkey: postkey, uid: userid) } }) } func savetagid(tagid: string, postkey: string, uid: string){ let posttag = [tagid: true] print("post tag\(posttag)") ref.updatechildvalues(["posts/\(postkey)/tagids": posttag, "user-posts/\(uid)/\(postkey)/tagids": posttag]) }
this database looks like. in tagids there should more 1 if there more 1 tag
{ "posts": { "-knlbs1ec6tkfla2y4sc": { "author": "michele", "post": "hello human", "tagids": { "-knlbs76y5i0kdv1oxs_": true }, "tagnames": "test, hope works", "uid": "qgacte84gbsc7uun4wemxcdhxwo2" }, "-knlcb1cacxxplpyc5ql": { "author": "michele", "post": "you're hope", "tagids": { "-knlcb723scwihyduwrn": true }, "tagnames": "help, obi wan", "uid": "qgacte84gbsc7uun4wemxcdhxwo2" } }, "tags": { "help": "-knlcb71o5l2phdqdl62", "hope works": "-knlbs76y5i0kdv1oxs_", "obi wan": "-knlcb723scwihyduwrn", "test": "-knlbs7509hq29waf8ms" } }
Comments
Post a Comment