1
There is no Firebase method to write to multiple paths at once. Some future tools planned by the team (e.g. Triggers) may resolve this in the future.
没有Firebase方法一次写入多个路径。团队计划的一些未来工具(例如触发器)可能在将来解决此问题。
This topic has been explored before and the firebase-multi-write README contains a lot of discussion on the topic. The repo also has a partial solution to client-only atomic writes. However, there is no perfect solution without a server process.
之前已经探讨了这个主题,firebase-multi-write README包含了很多关于该主题的讨论。 repo还有一个仅限客户端原子写入的部分解决方案。但是,没有服务器进程就没有完美的解决方案。
It's important to evaluate your use case and see if this really matters. If the second and third writes failed to write to a geo query, chances are, there's really no consequence. Most likely, it's essentially the same as if the first write had failed, or if all writes had failed; it won't appear in searches by geo location. Thus, the complexity of resolving this issue is probably a time sink.
评估您的用例非常重要,看看这是否真的很重要。如果第二次和第三次写入无法写入地理查询,则可能没有任何后果。最有可能的是,它与第一次写入失败或者所有写入都失败的情况基本相同;它不会出现在地理位置的搜索中。因此,解决这个问题的复杂性可能是一个时间问题。
Of course, it does cost a few bytes of storage. If we're working with millions of records, that may matter. A simple solution for this scenario would be to run and audit report that detects broken links between the data and geofire tables and cleans up old data.
当然,它确实需要几个字节的存储空间。如果我们正在处理数百万条记录,那可能很重要。这种情况的一个简单解决方案是运行和审计报告,检测数据和地理表格之间断开的链接并清理旧数据。
If an atomic operation is really necessary, such as gaming mechanics where fairness or cheating could be an issue, or where integrity is lost by having partial results, there are a couple options:
如果确实需要原子操作,例如公平或作弊可能成为问题的游戏机制,或者通过获得部分结果而导致完整性丢失,则有以下几种选择:
1) Master Record approach
1)Master Record方法
Pick a master path (the one that must exist) and use security rules to ensure other records cannot be written, unless the master path exists.
选择主路径(必须存在的路径)并使用安全规则以确保无法写入其他记录,除非存在主路径。
".write": "root.child('maste_path').child(newData.child('master_record_id')).exists()"
2) Server-side script approach
2)服务器端脚本方法
Instead of writing the paths separately, use a queue strategy.
不使用单独编写路径,而是使用队列策略。
- Create an single event by writing a single event to a queue
通过将单个事件写入队列来创建单个事件
- Have a server-side process monitor the queue and process events
让服务器端进程监视队列并处理事件
- The server-side process does the multiple writes and ensures they all succeed
服务器端进程执行多次写入并确保它们都成功
- If any fail, the server-side process handles rollbacks or retries
如果有任何失败,服务器端进程将处理回滚或重试
By using the server-side queue, you remove the risk of a client going offline between writes. The server can safely survive restarts and retry events or failures when using the queue model.
通过使用服务器端队列,可以消除客户端在写入之间脱机的风险。使用队列模型时,服务器可以安全地重新启动并重试事件或失败。