Я понимаю, что 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());
}
}
}