java - 按 TimeStamp 升序排列 Firestore 数据
问题描述
我将应用程序发布的想法存储在 Firestore 中。数据像这样的Ideas/{documentID}/IdeaObject存储在 Firestore 中。问题是当我检索数据时,它在发布时没有排序。检索到的想法是根据 Firestore 自动创建的 documentID 的 id。我在我的模型类中使用了 ServerTimestamp,当我检索它时,我使用orderBy
我的 Firestore 引用的方法,但仍然没有。
想法.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 值升序排列。
解决方案
timeStamp
在查询数据库而不是 Date () 时,您不能使用 String ( ),date
并且期望其行为与日期相同。所以要解决这个问题,请更改以下代码行:
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
至
firestoreDb.collection("ideas")
.orderBy("date", Query.Direction.ASCENDING)
为了使它工作,这种查询需要一个索引。要创建一个,请从以下帖子中查看我的答案:
推荐阅读
- python-3.x - Get list of items with wikipedia in python
- android - view.requestLayout - setting layout of a button programmatically
- wordpress - 使用插件的自动导演网格列表
- html - onBlur 上的客户端验证
- java - 所有声明为类变量的数组都应该是静态的吗?
- pandas - pandas dataframe groupby a col 如果按行分组中存在值,则将相同的值分配给组行或从外部分配最大值并递增
- reactjs - 使用 laravel 配置 react SASS .module.scs 文件的问题
- javascript - 使用 JQuery 隐藏字符串开头的文本
- javascript - 有什么方法可以防止将 html div 的内容复制或将带有其样式的文本输入到剪贴板?
- ios - iOS 框架 XLPagerTabStrip 无法正常工作