gpt4 book ai didi

java - 无法使用 Cloud Firestore 按时间顺序排序数据

转载 作者:行者123 更新时间:2023-12-02 10:35:58 24 4
gpt4 key购买 nike

我了解 Cloud Firestore 不会自动按顺序排序数据。我创建了一个值,它本质上是将纪元转换为小时。我想按此值对查询的数据进行排序:

    Query queryStore = FirebaseFirestore.getInstance()
.collection(POLLS_LABEL)
.orderBy("epoch", Query.Direction.DESCENDING);

我发现我的数据没有按照我希望的方式排序。不确定我是否需要在代码中添加特殊注释,但我想它就像上面一样简单。以下是我的模型:

@com.google.firebase.firestore.IgnoreExtraProperties
public class Poll {

private String question;
private String image_URL;
private String user_ID;
private String display_name;
private int vote_count;
private Long epoch;
private long trend_score;
@ServerTimestamp
private Date date;

private ArrayList<String> answers;

public Poll() {
}

public Poll(String Question, String Image_URL, ArrayList<String> answers, int vote_count, String UserID, String DisplayName, Long epoch, long trend_score, Date date
) {
this.question = Question;
this.image_URL = Image_URL;
this.answers = answers;
this.vote_count = vote_count;
this.user_ID = UserID;
this.display_name = DisplayName;
this.epoch = epoch;
this.trend_score = trend_score;
this.date = date;
}

public String getUser_id() {
return user_ID;
}

public String getDisplay_name() {
return display_name;
}

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public String getImage_URL() {
return image_URL;
}

public Long getEpoch() {
return epoch;
}

public void setEpoch(Long epoch) {
this.epoch = epoch;
}

public void setUser_id(String user_id) {
this.user_ID = user_id;
}

public void setDisplay_name(String display_name) {
this.display_name = display_name;
}

public void setImage_URL(String image_URL) {
this.image_URL = image_URL;
}

public Integer getVote_count() {
return vote_count;
}

public void setVote_count(Integer vote_count) {
this.vote_count = vote_count;
}

public long getTrend_score() {
return trend_score;
}

public void setTrend_score(long trend_score) {
this.trend_score = trend_score;
}

@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("question", question);
result.put("image_URL", image_URL);
result.put("vote_count", 0);
result.put("user_ID", user_ID);
result.put("display_name", display_name);
result.put("epoch", epoch);
result.put("trend_score", trend_score);
return result;
}

创作:

final Poll poll = new Poll(mCreatePollQuestion.getText().toString(), resultImageURL, mPollAnswers, 0, ID, displayName, currentEpoch, intialQS, FieldValue.serverTimestamp());

添加到 Firestore:

      mStoreBaseRef.collection(POLLS).add(poll).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
DocumentReference docRef = task.getResult();
String key = docRef.getId();
Log.v("KEY", key);
Toast.makeText(getApplicationContext(), key, Toast.LENGTH_LONG).show();
CollectionReference pollAnswerRef = mStoreBaseRef.collection("Polls").document(key).collection("answers");
//TODO: need to add answers
for (int i = 0; i < mPollAnswers.size(); i++){
pollAnswerRef.document(String.valueOf(i + 1)).set((poll.answerConvert(mPollAnswers, i)), SetOptions.merge());
}

}
}

最佳答案

您猜对了,您应该使用注释,但您的 epoch 对象不应Long 类型,而是 Date。因此,在您的 Poll 类中,请更改以下代码行:

private Long epoch;

@ServerTimestamp
private Date date;

我将该date 对象命名为date,因为该对象的类型是Date 而不再是纪元。当您创建Pool类的对象时,不需要设置日期。 Firebase 服务器将读取您的日期字段,因为它是 ServerTimestamp(请参阅注释),并相应地使用服务器时间戳填充该字段。

添加日期的另一个解决方案是使用MapFieldValue.serverTimestamp()像这样:

Map<String, Object> map = new HashMap<>();
map.put("date", FieldValue.serverTimestamp());
docRef.update(map).addOnCompleteListener(new OnCompleteListener<Void>() {/* ... */}

关于java - 无法使用 Cloud Firestore 按时间顺序排序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53292851/

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