Home > Net >  Firebase Database Rest Api: How to get the next page when use LimitToLast()
Firebase Database Rest Api: How to get the next page when use LimitToLast()

Time:12-05

I have asked several questions about the use of LimitToLast() And EndAt() Also The Paginating backward from the end of The Data, Because I am trying to fetch the first 10 most viewed data using OrderBy("Views") Then Get the other 10 When Scroll Down.

When fetching the first 10 data Ordered By Views, I get it correctly, but my problem is how do I get the Correct Node so as I can use it to get the (Previous Page) which contains the other 10 data without any problem even if there are nodes with the same value of Views.

As it is assumed that we have to get the node that has the lowest value of Views and store it in string and when fetching the other 10 data we use it like this : OrderBy("Views").LimitToLast(11).EndAt(LastValue,LastId) But I always get incorrect and often duplicate results, especially if there is data with the same value of Views. My Json Stracture 30 Nodes :

{
  "-Mps4qWpgU-L5E3OfAMD" : {
    "Text" : 1,
    "Views" : 20
  },
  "-Mps4qoSPhzh3Hzu1i2y" : {
    "Text" : 2,
    "Views" : 20
  },
  "-Mps4qxhzwbQ80snD3jF" : {
    "Text" : 3,
    "Views" : 20
  },
  "-Mps4r60gkcsFjI33d-q" : {
    "Text" : 4,
    "Views" : 20
  },
  "-Mps4rF4Ku1X1FIyR0M8" : {
    "Text" : 5,
    "Views" : 20
  }
 More...
}

My C# Code:

    public class PostData
    {
        public int Views{get;set;}
        public string Text{get;set;}
    }
//Get First 10
var Data = await firebaseclient
                .Child("Education/")
                .OrderBy("Views")
                .LimitToLast(10)
                .OnceAsync<PostData>();
                //Order The Data By Descending on their Views And Key
                var sortedData = Data.OrderByDescending(x=>x.Object.Views).ThenByDescending(x=>x.Key);
                //Save The Last Id (lowest value of Views) and his Value Of Views
               string LastID = sortedData.Last().Key;
               int LastValue = sortedData.Last().Object.Views;

//Get More (Previous Page)
var Data = await firebaseclient
                .Child("Education/")
                .OrderBy("Views")
                .EndAt(LastValue,LastID)
                .LimitToLast(11)
                .OnceAsync<PostData>();
                //Order The Data By Descending on their Views And Key
                var sortedData = Data.OrderByDescending(x=>x.Object.Views).ThenByDescending(x=>x.Key);
                //Update The Last Id and his Value Of Views
               string LastID = sortedData.Last().Key;
               int LastValue = sortedData.Last().Object.Views;

Thanks in advance and sorry for the long post :)

CodePudding user response:

This quick jsbin shows that the correct order for the nodes is:

"-Mps4qWpgU-L5E3OfAMD": {"Text":1,"Views":20},
"-Mps4qoSPhzh3Hzu1i2y": {"Text":2,"Views":20},
"-Mps4qxhzwbQ80snD3jF": {"Text":3,"Views":20},
"-Mps4r60gkcsFjI33d-q": {"Text":4,"Views":20},
"-Mps4rF4Ku1X1FIyR0M8": {"Text":5,"Views":20},
"-Mps4rOgbcEeIZ-Kyi0I": {"Text":6,"Views":20},
"-Mps4rYAe5dIPytLvk4E": {"Text":7,"Views":20},
"-Mps4rgdzKlylkoeOTGc": {"Text":8,"Views":20},
"-Mps4rpqUdlVdfsatirv": {"Text":9,"Views":20},
"-Mps4rzCa-YS60CqXzfy": {"Text":10,"Views":20},
"-Mps4s7am1ZCuLtBp0nk": {"Text":11,"Views":20},
"-Mps4sGxBOgFsTLFSqMk": {"Text":12,"Views":20},
"-Mps4sQ9CLYNFQ4Uynhb": {"Text":13,"Views":20},
"-Mps4sZY5egXpf5_CmFi": {"Text":14,"Views":20},
"-Mps4shiJxx2dQVwen-Y": {"Text":15,"Views":20},
"-Mps4srZmVvPvImOf48D": {"Text":16,"Views":20},
"-Mps4tQpS0qYHlwHd8jJ": {"Text":18,"Views":20},
"-Mps4tZujfdt-q1EnjGq": {"Text":19,"Views":20},
"-Mps4thzzjBtFaRyRcxu": {"Text":20,"Views":20},
"-Mps4tr7SE6t7qZc2513": {"Text":21,"Views":20},
"-Mps4u0-6dOElpUhjzYY": {"Text":22,"Views":20},
"-Mps4u9G_112glr6ijQl": {"Text":23,"Views":20},
"-Mps4uJ-j75wAdmth6lb": {"Text":24,"Views":20},
"-Mps4uS8BCBb7FASfZJ5": {"Text":25,"Views":20},
"-Mps4uaMoWvGEvKNCcVc": {"Text":26,"Views":20},
"-Mps4ujkjYW5CeqSFPqU": {"Text":27,"Views":20},
"-Mps4usz_FyTYx49oz6e": {"Text":28,"Views":20},
"-Mps4v1HhVdmlOOZhYs_": {"Text":29,"Views":20},
"-Mps4vB9i4F0irpPeRgz": {"Text":30,"Views":20},
"-Mps4tH5mnQlklXP5o3I": {"Text":17,"Views":50}

If we order by Views and take the last 10 notes with this query: orderBy="Views"&limitToLast=10 we get these results:

{
  "-Mps4tH5mnQlklXP5o3I" : { "Text" : 17, "Views" : 50 },
  "-Mps4u0-6dOElpUhjzYY" : { "Text" : 22, "Views" : 20 },
  "-Mps4u9G_112glr6ijQl" : { "Text" : 23, "Views" : 20 },
  "-Mps4uJ-j75wAdmth6lb" : { "Text" : 24, "Views" : 20 },
  "-Mps4uS8BCBb7FASfZJ5" : { "Text" : 25, "Views" : 20 },
  "-Mps4uaMoWvGEvKNCcVc" : { "Text" : 26, "Views" : 20 },
  "-Mps4ujkjYW5CeqSFPqU" : { "Text" : 27, "Views" : 20 },
  "-Mps4usz_FyTYx49oz6e" : { "Text" : 28, "Views" : 20 },
  "-Mps4v1HhVdmlOOZhYs_" : { "Text" : 29, "Views" : 20 },
  "-Mps4vB9i4F0irpPeRgz" : { "Text" : 30, "Views" : 20 }
}

When we order them the same as above, the first one of these results in the original list is:

"-Mps4u0-6dOElpUhjzYY": {"Text":22,"Views":20},

So those are the value and key you need to use in your subsequent call to get the page above the last one: orderBy="Views"&limitToLast=10&endAt=20,"-Mps4u0-6dOElpUhjzYY". As far as I can see this gives the expected results.

  • Related