【发布时间】:2018-10-17 21:01:02
【问题描述】:
我将应用程序发布的想法存储在 Firestore 中。数据像 Ideas/{documentID}/IdeaObject 一样存储在 Firestore 中。问题是当我检索数据时,它没有按发布时间排序。检索到的想法是根据 Firestore 自动创建的 documentID 的 id。我在我的模型类中使用了 ServerTimestamp,当我检索它时,我将 orderBy 方法与我的 Firestore 引用一起使用,但仍然没有。
Idea.java
public class Idea {
@ServerTimestamp
private Date date;
private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
private int views, favorites;
private ArrayList<String> lookingFor = new ArrayList<>();
private ArrayList<String> tags = new ArrayList<>();
private String userID;
private String timeStamp;
public Idea() {
}
public Idea(String title, String idea, String timeCommitment, String ideaStage, String postedBy, String website, String videoPitch, int views, int favorites, ArrayList<String> lookingFor, ArrayList<String> tags, String userID, String timeStamp) {
this.title = title;
this.idea = idea;
this.timeCommitment = timeCommitment;
this.ideaStage = ideaStage;
this.postedBy = postedBy;
this.website = website;
this.videoPitch = videoPitch;
this.views = views;
this.favorites = favorites;
this.lookingFor = lookingFor;
this.tags = tags;
this.userID = userID;
this.timeStamp = timeStamp;
}
创意发布方式
private void postIdea() {
final String ideaID = UUID.randomUUID().toString();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Idea ideas = new Idea(title, idea, timeCommitment, ideaStage, AppValues.fullName, website, videoPitch, 0, 0, lookingFor, tags, AppValues.userId, "" + timestamp.getTime());
firestoreDb.collection("ideas")
.document(ideaID)
.set(ideas)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
postIdeaUser(ideaID);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
hideLoadingDialog();
showToast(e.getMessage());
}
});
}
按时间检索所有创意
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ideaArrayList = new ArrayList<>();
ideaArrayList.clear();
for (DocumentSnapshot documentSnapshot : task.getResult()) {
Idea idea = documentSnapshot.toObject(Idea.class);
if (!idea.getUserID().equals(AppValues.userId)) {
ideaArrayList.add(idea);
}
}
callAdapter();
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
progressBar.setVisibility(View.GONE);
swipeRefresh.setEnabled(true);
errorText.setVisibility(View.VISIBLE);
}
}
});
我想要实现的是检索所有想法并让它们按 TimeStamp 值升序排列。
【问题讨论】:
-
你为什么要同时存储一个名为
date并带有@ServerTimestamp注释的字段和一个格式化的timeStamp字符串?似乎您只需要@ServerTimestamp值,并让您的查询使用它。 -
@DougStevenson 您能否指定如何在查询中使用 serverTimestamp 值?我在检索 data.orderBy("date", Query.Direction.ASCENDING) 时尝试使用它,但仍然没有。
-
我希望它可以工作。
-
“无”是什么意思?
task.getException()会返回什么吗?
标签: java android firebase timestamp google-cloud-firestore