在Android中添加对Firebase查询的限制?

时间:2017-01-01 22:49:49

标签: android firebase firebase-realtime-database firebase-security

我的应用加载了列表视图上用户共享的链接。我尝试一次加载10个链接/帖子,一旦用户到达最终查询再添加10个链接/帖子,依此类推。

我已对数据进行了非规范化,因此,一旦发布帖子,就会在"/links""user-links/$userId/"下通过以下代码创建:

        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数据

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
    },
  },  
}

这是我目前根据其创建时间检索数据的方式:

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

其中; int QUERY_LIMIT = 10;

Firebase规则

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

问题:

1)一旦用户到达终点,如何跳过前10个链接(已经加载)并加载下一个10个链接?

2)有没有办法分配升序或降序?我想首先发布最新帖子。

3)firebase-rules ".indexOf"是解决方案吗?如果是这样,有人可以分享我必须写的确切规则吗?

注意:createdAt是在最初创建数据时由以下代码生成的:postValues.put("createdAt", ServerValue.TIMESTAMP );

0 个答案:

没有答案