gpt4 book ai didi

android - 在我的 Android 应用程序中使用 ServerValue.TIMESTAMP

转载 作者:行者123 更新时间:2023-11-29 15:42:12 26 4
gpt4 key购买 nike

我已经阅读了很多与 stackoverflow 相关的问题

ServerValue.TIMESTAMP

但我不知道如何在我的应用中使用它。

我需要获取创建帖子的时间戳。时间戳应添加到与帖子的 uid 、作者等相同的位置。

写帖子到 firebase 数据库的代码 fragment :

 private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

mDatabase.updateChildren(childUpdates);
}

我的Post.java文件

@IgnoreExtraProperties
public class Post {

public String uid;
public String author;
public String title;
public String body;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public Long timeStamp;

public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}

public Post(String uid, String author, String title, String body) {
this.uid = uid;
this.author = author;
this.title = title;
this.body = body;
}

// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("author", author);
result.put("title", title);
result.put("body", body);
result.put("starCount", starCount);
result.put("stars", stars);

return result;
}
// [END post_to_map]

我应该如何使用

ServerValue.TIMESTAMP 

在我的代码中获取帖子的创建时间。

最佳答案

ServerValue.TIMESTAMP只是一个占位符值。当 Firebase 服务器处理更新请求并找到 ServerValue.TIMESTAMP 作为值时,它会将其替换为当前服务器时钟。

在您的 writeNewPost() 方法中,您可以添加一行来设置创建时间:

Map<String, Object> postValues = post.toMap();
postValues.put("timeStamp", ServerValue.TIMESTAMP);

如果您使用 Post.toMap() 只是为了创建帖子,而不是为了更新,您可以将类似的语句放在那里而不是在 writeNewPost() 中。

关于android - 在我的 Android 应用程序中使用 ServerValue.TIMESTAMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com