服务器时间戳字段添加到要添加的对象
问题说明
我有一个Challenge
对象,该对象具有它自己的属性,并且可以像这样将其成功添加到数据库中:
I have a Challenge
object, which has it's own properties and I'm able to add it to the database successfully like this:
DocumentReference challengeRef=usersRef.document(loggedUserEmail).collection("challenges_feed").
document(callengeID);
challengeRef.set(currentChallenge);
这是数据库中的样子:
我想在数据库中创建一个名为latestUpdateTimetamp
的新字段(在此挑战下).这是应该的样子(我已经手动添加了):
I'd like to make a new field in the database (under this challenge) which called latestUpdateTimetamp
. This is how it should look like (I have added it manually):
我试图像这样在object
的constructor
中设置它:
I have tried to set it in the constructor
of the object
like this:
private Map<String,String> latestUpdateTimestamp;
public Challenge(String id, String senderName, String senderEmail) {
this.senderName=senderName;
this.senderEmail = senderEmail;
this.latestUpdateTimestamp= ServerValue.TIMESTAMP;
}
但这就是我在database
中得到的:
But this is what I get in the database
:
我正在尝试在同一调用中将latestUpdateTimestamp
添加到Challenge
并将Challenge
对象本身添加到数据库.有可能吗?
I'm trying to add the latestUpdateTimestamp
to the Challenge
and the Challenge
object itself to the database at the same call. Is it possible?
我可以以某种方式将此timestamp
作为属性添加到此object
之前吗?
Can I somehow add this timestamp
as a property to this object
before adding it?
我知道我可以打一个新电话并添加此字段,但是我想知道是否有可能一次.
I know I'm able to make a new call and add this field, but I'm wondering if it's possible at once.
正确答案
是的,可以使用Map
.首先,根据官方文档,使用如下所示的注释将是必要的:
Yes you can, using a Map
. First of all, according to official docs it will be necessary to use an annotation that looks like this:
@ServerTimestamp Date time;
用于标记要用服务器时间戳填充的日期"字段的注释.如果正在写入的POJO包含@ServerTimestamp注释字段的null,它将被服务器生成的时间戳替换.
Annotation used to mark a Date field to be populated with a server timestamp. If a POJO being written contains null for a @ServerTimestamp-annotated field, it will be replaced with a server-generated timestamp.
这是如何同时用服务器时间戳更新latestUpdateTimestamp
字段和使用所需的值更新challangeId
的方法.
This is how you can update the latestUpdateTimestamp
field with the server timestamp and the challangeId
with the desired value at the same time.
DocumentReference senderRef = challengeRef
.document(loggedUserEmail)
.collection("challenges_feed")
.document(callengeID);
Map<String, Object> updates = new HashMap<>();
updates.put("latestUpdateTimestamp", FieldValue.serverTimestamp());
updates.put("challangeId", "newChallangeId");
senderRef.update(updates).addOnCompleteListener(new OnCompleteListener<Void>() {/* ... */}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcebbhk
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
使用c++17更新时出现G++编译器警告
it1352 06-18 -
复制文件夹/文件而不修改属性?
it1352 07-15