大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中向Firebase数组添加项目而不首先观察数组,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
REF_USER.child(UID).observeSingleEventOfType(.Value,withBlock: { snapshot in if !snapshot.exists() {return} if let Dict = snapshot.value as? Dictionary<String,AnyObject>,let posts = Dict["posts" as? [String] { posts.append(newPost) REF_USER.child(UID + "/posts").SETVALue(posts) } }
有没有办法跳过观察步骤,并立即更新数组中的帖子?假设,像:
REF_USER.child(UID + "/posts").addToArray(newPost)
不确定为什么要通过问题中列出的步骤添加新帖子,但这是另一种解决方案:
thisPostRef = postsRef.childByAutoId //create a new post node thisPostRef.SETVALue("here's a new post") //store the post in it
编辑:
这将产生这样的结构
posts post_id_0: "here's a new post" post_id_1: "another post" post_id_2: "cool post"
这种结构避免了数组的缺陷.
另一个编辑. OP询问如何将其写入users / UID / posts节点
usersRef = rootRef.childByAppendingPath("users") thisUserRef = usersRef.childByAppendingPath(the users uid) thisUserPostRef = thisUserRef.childByAutoId //create a new post node thisUserPostRef.SETVALue("here's a new post") //store the post in it
以上是大佬教程为你收集整理的ios – 在Swift中向Firebase数组添加项目而不首先观察数组全部内容,希望文章能够帮你解决ios – 在Swift中向Firebase数组添加项目而不首先观察数组所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。