且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android Firebase数据库事务

更新时间:2022-05-30 22:59:24

您应该期望事务中的可用数据的初始快照在首次执行期间可以为null.请注意文档:

You should be expecting that the initial snapshot of data available in your transaction could be null during its first execution. Note this from the documentation:

注意:由于doTransaction()被多次调用,因此必须 能够处理空数据.即使您的数据库中已有数据 远程数据库,在事务处理时可能未在本地缓存 函数运行,结果为空.

Note: Because doTransaction() is called multiple times, it must be able to handle null data. Even if there is existing data in your remote database, it may not be locally cached when the transaction function is run, resulting in null for the initial value.

也在 javadoc 中它说:

此方法可能会被当前方法多次调用 数据在此位置.

This method will be called, possibly multiple times, with the current data at this location.

预计第一次运行处理程序时,它将处理一个空数据库.然后,当已知数据(如果有)时,您也应该准备处理这种情况.如果您不想在该位置没有数据(或未知数据)的地方做任何事情,只需返回一个成功的Transaction,而无需更改mutableData.

Expect that the first time your handler is run, it will be dealing with an empty database. Then, when the data there is known (if any), you should be prepared to handle that case also. If you don't want to do anything where there is no data (or unknown data) at the location, simply return a successful Transaction with no changes to mutableData.