HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中向Firebase数组添加项目而不首先观察数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目前,我首先通过观察数组,附加新帖子,然后更新ref来向我的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)

解决方法

通常很好的做法是避免使用Firebase中的数组,因为它们非常难以处理;单个元素无法直接访问,无法更新 – 必须重新编写.

不确定为什么要通过问题中列出的步骤添加新帖子,但这是另一种解决方案:

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,请注明来意。