Display posts in descending posted order

前端 未结 18 884
南方客
南方客 2020-11-22 11:47

I\'m trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child         


        
相关标签:
18条回答
  • 2020-11-22 12:15

    Since firebase 2.0.x you can use limitLast() to achieve that:

    fbl.child('sell').orderByValue().limitLast(20).on("value", function(fbdataSnapshot) { 
      // fbdataSnapshot is returned in the ascending order
      // you will still need to order these 20 items in
      // in a descending order
    }
    

    Here's a link to the announcement: More querying capabilities in Firebase

    0 讨论(0)
  • 2020-11-22 12:17

    You are searching limitTolast(Int x) .This will give you the last "x" higher elements of your database (they are in ascending order) but they are the "x" higher elements

    if you got in your database {10,300,150,240,2,24,220}

    this method:

    myFirebaseRef.orderByChild("highScore").limitToLast(4)
    

    will retrive you : {150,220,240,300}

    0 讨论(0)
  • 2020-11-22 12:19

    Since this answer was written, Firebase has added a feature that allows ordering by any child or by value. So there are now four ways to order data: by key, by value, by priority, or by the value of any named child. See this blog post that introduces the new ordering capabilities.

    The basic approaches remain the same though:

    1. Add a child property with the inverted timestamp and then order on that.

    2. Read the children in ascending order and then invert them on the client.

    Firebase supports retrieving child nodes of a collection in two ways:

    • by name
    • by priority

    What you're getting now is by name, which happens to be chronological. That's no coincidence btw: when you push an item into a collection, the name is generated to ensure the children are ordered in this way. To quote the Firebase documentation for push:

    The unique name generated by push() is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.

    The Firebase guide on ordered data has this to say on the topic:

    How Data is Ordered

    By default, children at a Firebase node are sorted lexicographically by name. Using push() can generate child names that naturally sort chronologically, but many applications require their data to be sorted in other ways. Firebase lets developers specify the ordering of items in a list by specifying a custom priority for each item.

    The simplest way to get the behavior you want is to also specify an always-decreasing priority when you add the item:

    var ref = new Firebase('https://your.firebaseio.com/sell');
    var item = ref.push();
    item.setWithPriority(yourObject, 0 - Date.now());
    

    Update

    You'll also have to retrieve the children differently:

    fbl.child('sell').startAt().limitToLast(20).on('child_added', function(fbdata) {
      console.log(fbdata.exportVal());
    })
    

    In my test using on('child_added' ensures that the last few children added are returned in reverse chronological order. Using on('value' on the other hand, returns them in the order of their name.

    Be sure to read the section "Reading ordered data", which explains the usage of the child_* events to retrieve (ordered) children.

    A bin to demonstrate this: http://jsbin.com/nonawe/3/watch?js,console

    0 讨论(0)
  • 2020-11-22 12:20

    just use reverse() on the array , suppose if you are storing the values to an array items[] then do a this.items.reverse()

     ref.subscribe(snapshots => {
           this.loading.dismiss();
    
          this.items = [];
          snapshots.forEach(snapshot => {
    
            this.items.push(snapshot);
    
          });
          **this.items.reverse();**
        },
    
    0 讨论(0)
  • 2020-11-22 12:21

    I have a date variable (long) and wanted to keep the newest items on top of the list. So what I did was:

    • Add a new long field 'dateInverse'
    • Add a new method called 'getDateInverse', which just returns: Long.MAX_VALUE - date;
    • Create my query with: .orderByChild("dateInverse")
    • Presto! :p
    0 讨论(0)
  • 2020-11-22 12:21

    In Android there is a way to actually reverse the data in an Arraylist of objects through the Adapter. In my case I could not use the LayoutManager to reverse the results in descending order since I was using a horizontal Recyclerview to display the data. Setting the following parameters to the recyclerview messed up my UI experience:

    llManager.setReverseLayout(true);
    llManager.setStackFromEnd(true);
    

    The only working way I found around this was through the BindViewHolder method of the RecyclerView adapter:

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final SuperPost superPost = superList.get(getItemCount() - position - 1);
    }
    

    Hope this answer will help all the devs out there who are struggling with this issue in Firebase.

    0 讨论(0)
提交回复
热议问题