• <tfoot id='Q2AYR'></tfoot>
  • <small id='Q2AYR'></small><noframes id='Q2AYR'>

    <i id='Q2AYR'><tr id='Q2AYR'><dt id='Q2AYR'><q id='Q2AYR'><span id='Q2AYR'><b id='Q2AYR'><form id='Q2AYR'><ins id='Q2AYR'></ins><ul id='Q2AYR'></ul><sub id='Q2AYR'></sub></form><legend id='Q2AYR'></legend><bdo id='Q2AYR'><pre id='Q2AYR'><center id='Q2AYR'></center></pre></bdo></b><th id='Q2AYR'></th></span></q></dt></tr></i><div id='Q2AYR'><tfoot id='Q2AYR'></tfoot><dl id='Q2AYR'><fieldset id='Q2AYR'></fieldset></dl></div>

        <bdo id='Q2AYR'></bdo><ul id='Q2AYR'></ul>
        <legend id='Q2AYR'><style id='Q2AYR'><dir id='Q2AYR'><q id='Q2AYR'></q></dir></style></legend>

        按 T​​imeStamp 升序排列 Firestore 数据

        时间:2023-07-25
        <i id='wXuGu'><tr id='wXuGu'><dt id='wXuGu'><q id='wXuGu'><span id='wXuGu'><b id='wXuGu'><form id='wXuGu'><ins id='wXuGu'></ins><ul id='wXuGu'></ul><sub id='wXuGu'></sub></form><legend id='wXuGu'></legend><bdo id='wXuGu'><pre id='wXuGu'><center id='wXuGu'></center></pre></bdo></b><th id='wXuGu'></th></span></q></dt></tr></i><div id='wXuGu'><tfoot id='wXuGu'></tfoot><dl id='wXuGu'><fieldset id='wXuGu'></fieldset></dl></div>

          • <tfoot id='wXuGu'></tfoot>
            <legend id='wXuGu'><style id='wXuGu'><dir id='wXuGu'><q id='wXuGu'></q></dir></style></legend>
                <tbody id='wXuGu'></tbody>
            1. <small id='wXuGu'></small><noframes id='wXuGu'>

              • <bdo id='wXuGu'></bdo><ul id='wXuGu'></ul>
                  本文介绍了按 T​​imeStamp 升序排列 Firestore 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我将应用程序发布的想法存储在 Firestore 中.数据像 Ideas/{documentID}/IdeaObject 一样存储在 Firestore 中.问题是当我检索数据时,它没有按发布时间排序.检索到的想法是根据 Firestore 自动创建的 documentID 的 id.我在我的模型类中使用了 ServerTimestamp,当我检索它时,我将 orderBy 方法与我的 Firestore 引用一起使用,但仍然没有.

                  I am storing Ideas posted by the application in Firestore. The data is stored in Firestore like this Ideas/{documentID}/IdeaObject. The issue is when I retrieve the data it is not sorted w.r.t time it was posted. The ideas that are retrieved are in according to the id's of their documentID which is automatically create by Firestore. I have used ServerTimestamp in my Model Class and also when I retrieve it, I use the orderBy method with my Firestore reference but still nothing.

                  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 值升序排列.

                  What I want to achieve is retrieve all the ideas and have them ordered ascending by the TimeStamp value.

                  推荐答案

                  查询数据库时不能使用字符串(timeStamp)而不是日期(date)并期望表现得像约会一样.所以要解决这个问题,请更改以下代码行:

                  You cannot use a String (timeStamp) when querying your database instead of a Date (date) and expect to behave as it was a date. So to solve this, please change the following line of code:

                  firestoreDb.collection("ideas")
                              .orderBy("timeStamp", Query.Direction.ASCENDING)
                  

                  firestoreDb.collection("ideas")
                              .orderBy("date", Query.Direction.ASCENDING)
                  

                  为了使它工作,这种查询需要一个索引.要创建一个,请查看我在以下帖子中的回答:

                  To make it work, this kind of query requires an index. To create one, please check my answer from the following post:

                  • Firestore whereEqualTo、orderBy 和 limit(1) 不起作用

                  这篇关于按 T​​imeStamp 升序排列 Firestore 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Hibernate 在读取 Java 日历对象并将其写入 SQL TIME 下一篇:创建没有 timeZone 的时间戳

                  相关文章

                  最新文章

                • <tfoot id='g4JOg'></tfoot>

                  <small id='g4JOg'></small><noframes id='g4JOg'>

                  <legend id='g4JOg'><style id='g4JOg'><dir id='g4JOg'><q id='g4JOg'></q></dir></style></legend>
                  • <bdo id='g4JOg'></bdo><ul id='g4JOg'></ul>

                    1. <i id='g4JOg'><tr id='g4JOg'><dt id='g4JOg'><q id='g4JOg'><span id='g4JOg'><b id='g4JOg'><form id='g4JOg'><ins id='g4JOg'></ins><ul id='g4JOg'></ul><sub id='g4JOg'></sub></form><legend id='g4JOg'></legend><bdo id='g4JOg'><pre id='g4JOg'><center id='g4JOg'></center></pre></bdo></b><th id='g4JOg'></th></span></q></dt></tr></i><div id='g4JOg'><tfoot id='g4JOg'></tfoot><dl id='g4JOg'><fieldset id='g4JOg'></fieldset></dl></div>