0

My app loads links shared by a user on a listview. I'm trying to load 10-links/posts at a time, and once the user reaches the end query for 10 more links/posts, and so on.

I've denormalized the data, so that once a post is made, its created under "/links" and "user-links/$userId/" by the following code:

        String key = mDatabase.child("links").push().getKey();
        Map<String, Object> childUpdates = new HashMap<>();
        childUpdates.put("/links/" + key, postValues);
        childUpdates.put("/user-links/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/" + key, postValues);

Firebase Data

links
{
  "pushId1": {
    "userId"    : YuPfsnJD, //userId1
    "createdAt" : 1483309151261,
    "link"      : https://www.youtube.com/watch?v=bwbwTKXlvQA
  },

  "pushId2": {
    "userId"    : zZio89,  //userId2
    "createdAt" : 1483309145896,
    "link"      : https://firebase.google.com/docs/database/android/lists-of-data
  },

  "pushId3": {
    "userId"    : YuPfsnJD, //userId1
    "createdAt" : 1483309155525,
    "link"      : https://firebase.google.com/docs/database/security/indexing-data
  }
}

user-links
{
  "userId1": 
  {
    "pushId1": 
    {
        "userId"    : YuPfsnJD, //userId1
        "createdAt" : 1483309151261,
        "link"      : https://www.youtube.com/watch?v=bwbwTKXlvQA
    },

    "pushId3": 
    {
        "userId"    : YuPfsnJD, //userId1
        "createdAt" : 1483309155525,
        "link"      : https://firebase.google.com/docs/database/security/indexing-data
    },

  },

  "userId2": 
  {
    "pushId2": 
    {
        "userId"    : zZio89,  //userId2
        "createdAt" : 1483309145896,
        "link"      : https://firebase.google.com/docs/database/android/lists-of-data
    },
  },  
}

This is how I currently retrieve the data based on the time its createdAt:

Query myTopPostsQuery = mDatabase
                        .child("user-link")
                        .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                        .orderByChild("createdAt")
                        .limitToFirst(QUERY_LIMIT);

Where; int QUERY_LIMIT = 10;

Firebase Rules

{
  "rules": 
  {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Questions:

1) Once the user reaches the end, how do I skip the first 10 links (which have already been loaded) and load the next 10-links?

2) Is there a way to assign Ascending or Descending order? I would like to show the latest post first.

3) Is firebase-rules ".indexOf" the solution? If so can someone share the exact rule that I must write?

Note: createdAt is generated by the following code while creating the data initially: postValues.put("createdAt", ServerValue.TIMESTAMP );

Zen
  • 2,698
  • 4
  • 28
  • 41
  • as for the first question, did you mean like pagination? if you did, then check this http://stackoverflow.com/questions/37711220/firebase-android-pagination – koceeng Jan 02 '17 at 04:55
  • But in that case the videos are labelled as video1, video2 and so on. In my case these are $pushIds; which are server-generated keys. These $pushIds look like this: K_MfUhVQmeafsNJauqA – Zen Jan 02 '17 at 14:06
  • 1
    My point is on this code `.limitToFirst(mPageLimit).startAt(mPageEndOffset)`. Have you tried it? – koceeng Jan 02 '17 at 14:11
  • I have. It keeps loading the same posts again and again. – Zen Jan 02 '17 at 21:05

0 Answers0