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 );