Home > Net >  Firebase Rest Api:How To Use OrderBy() and StartAt() using The Last Key id
Firebase Rest Api:How To Use OrderBy() and StartAt() using The Last Key id

Time:11-27

I'm using firebase database rest API in my android application using c# And my application gets 10 data from the firebase database when the page is opened and these data are ordered by (Views Count) using .OrderBy("Views").LimitToLast(10)So far I am getting the data correctly and it works correctly, as well as I store the last id in a public string because I get another 10 elements from firebase database when user scroll down the listview, But when I get the other 10 data and use .StartAt(LastId).LimitToLast(10) the firebase will return null (0) data,Because the keys that return are not sorted correctly when I use .OrderBy("Views") So I can't get the correct last id to get the data starting at the last id, I found a way to order the keys using OrderByKey() And fetching the correct last id and fetching other 10 data starting at last id, **but I want to fetch the data ordered by (Views) **,

Summary: How can I get 10 elements from the firebase database ordered by ("Views") and at the same time I can get the other 10 elements starting at the last id for the first 10

My JSON Structure :

{
  "Main" : {
    "News" : {
      "Categories" : {
        "Education" : {
          "-Mn7-ZxkUPO01ifhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 5
          },
          "-Mn7-ZxkUPO01ifhtp11En" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 5
          },
          "-Mn7-ZxkUPO0112ifhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 12
          },
          "-Mn7-ZxkUPO01dxifhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 545
          },
          "-Mn7-ZxkUPO01sdifhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 5
          },
          "-Mn7-ZxkUPO01ifddhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 200
          },
          "-Mn7-ZxkUPO01ifdshtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 1
          },
          "-Mn7-ZxkUPO01ifhtdxpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 223
          },
          "-Mn7-ZxkUPO01ifhewtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 451
          },
          "-Mn7-ZxkUPO01idsdfhtpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 532
          },
          "-Mn7-ZxkUPO01ifh2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 1500
          },
          "-Mn7-ZxkUPOd01ifh2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 45
          },
          "-Mn7-ZxkUPO01ifh21tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 2
          },
          "-Mn7-ZxkUPO01ifh2dstpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 4
          },
          "-Mn7-ZxkUPO01ifh2txpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 4
          },
          "-Mn7-ZxkUPO01ifhds2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 2
          },
          "-Mn7-ZxkUPO01iddfh2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 33
          },
          "-Mn7-ZxkUPO01idxfh2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 52
          },
          "-Mn7-ZxkUPO01idsdfh2tpEn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 1
          },
          "-Mn7-ZxkUPO01ifh2tpEdsn" : {
            "Text" : "some text",
            "Title" : "some title",
            "Views" : 1000000
          }
        }
      }
    }
  }

C# Code:

        string LastId;
        public class NewsData
        {
            public string Title { set; get; }
            public string Text { set; get; }
            public int Views { set; get; }
        }
        public Page1()
        {
            InitializeComponent();
//work good and get the top 10 views
            var Data = await firebaseclient
               .Child("Main/News/Categories/Education")
               .OrderBy("Views")
               .LimitToLast(10)
               .OnceAsync<NewsData>();
                //Get Last id
                LastId = Data.Last().Key;
        }
public voud GetMore()
{
//this will return null data
               var Data = await firebaseclient
               .Child("Main/News/Categories/Education")
               .OrderBy("Views")
               .StartAt(LastId)
               .LimitToLast(10)
               .OnceAsync<NewsData>();
                //Update Last id
                LastId = Data.Last().Key;
}

Thanks in advance And I thank Frank van Puffelen because he helped me a lot :) in addition to that he published the JSON Response for tests here : Json Response

CodePudding user response:

As I said in my comments on your previous question: the Firebase Realtime Database REST API will return the correct, filtered nodes, but they will not be in any defined order - as the order of the keys in a JSON object is undefined by definition. There is nothing you can do to change that.

What you'll need to do is re-order the results in your application code, and then determine the first/last one. If you do that for the first/last of nodes you shared, you'll see that the first node has Views 33 and key -Mn7-ZxkUPO01iddfh2tpEn.

To get the previous page, you'd use:

https://stackoverflow.firebaseio.com/70102492/Main/News/Categories/Education.json?print=pretty&orderBy="Views"&limitToLast=10&endAt=33,"-Mn7-ZxkUPO01iddfh2tpEn"

So:

  • We endAt=33,"-Mn7-ZxkUPO01iddfh2tpEn", which means we end at the first item of the next page. We include both the value of Views and the key in this parameter, since there may be multiple nodes with Views equal to 33 and in that case the database will use the key to select the correct node to end at.
  • You might want to request 11 items instead of 10, given that you'e already shown the -Mn7-ZxkUPO01iddfh2tpEn.
  • Related