将元素追加到Firebase数组 [英] Append element to Firebase Array

查看:61
本文介绍了将元素追加到Firebase数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将元素添加到这样的数组中:

How I could append an element to an array like that:

使用此代码,我将覆盖旧数据:

Using this code I'm overriding the old data:

let toUpdate = [book.id]
self.refUsers.child(localUser.key!).child("booksPurchased").setValue(toUpdate, withCompletionBlock: { (error, _) in

推荐答案

在这种情况下,您将必须读取现有数据,然后使用添加的新值将其写回.如果要执行大量附加操作,则此类数组并非总是存储数据列表的最佳方法.为此,最好不要使用childByAutoId将数据推送到某个位置.

In this case, you will have to read the existing data, then write it back with the new value added. Arrays like this are not always the best way to store lists of data if you want to perform a lot of append operations. For that, you're better off pushing data into a location using childByAutoId.

这篇关于将元素追加到Firebase数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆