如何在Android中使用FieldValue.serverTimestamp()来定制模型类 [英] how to use FieldValue.serverTimestamp() to custom model class in android
本文介绍了如何在Android中使用FieldValue.serverTimestamp()来定制模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Firestore作为数据库,现在我想在用户注册时存储服务器时间戳。
来自Firestore文档
映射< String,Object> updates = new HashMap<>();
updates.put(timestamp,FieldValue.serverTimestamp());
docRef.update(updates)
.addOnCompleteListener(new OnCompleteListener< Void>(){// - }
但是现在我想在我的模型类中使用这个时间戳。
public class UserModel implements Serializable {
私有字符串名称;
私有字符串uid;
私有字符串电子邮件;
私有字符串密码;
私有长时间戳;
公共long getTimestamp(){
return timestamp;
}
public void setTimestamp(long timestamp){
this.timestamp = timestamp;
}
public String getName(){
return name;
}
$ b $ public void setName(String name){
this.name = name;
public String getUid(){
return uid;
}
public void setUid(String uid){
this.uid = uid;
}
public String getEmail(){
回复邮件;
}
public void setEmail(String email){
this.email = email;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
$ b我怎样才能做到这一点。
请帮助我。
解决方案经过大量研究,我找到了解决方案。
$ b
@ServerTimestamp
private日期时间戳;
$ b $ public Date getTimestamp(){
return timestamp;
}
public void setTimestamp(Date timestamp){
this.timestamp = timestamp;
}
当调用这个类时
UserModel userModel = new UserModel();
userModel.setEmail(email);
userModel.setPassword(password);
userModel.setName(name);
userModel.setUid(uid);
insertUserDataToFireStore(userModel);
这是我找到解决方案的链接
点击这里
I am using Firestore as a database and now i want to store server timestamp when user register.
from the Firestore doc
Map<String,Object> updates = new HashMap<>();
updates.put("timestamp", FieldValue.serverTimestamp());
docRef.update(updates)
.addOnCompleteListener(new OnCompleteListener<Void>() { //-- }
But now i want this time stamp in my model class.
public class UserModel implements Serializable {
private String name;
private String uid;
private String email;
private String password;
private long timestamp;
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
how can i achieve this.
Please help me.
解决方案 After lots of research i found a solution for this.
@ServerTimestamp
private Date timestamp;
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
and when calling this class
UserModel userModel = new UserModel();
userModel.setEmail(email);
userModel.setPassword(password);
userModel.setName(name);
userModel.setUid(uid);
insertUserDataToFireStore(userModel);
This is the link where i found the solution
Click here
这篇关于如何在Android中使用FieldValue.serverTimestamp()来定制模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文