Swift 无法将数据库时间戳与firebase';现在';安全规则

Swift 无法将数据库时间戳与firebase';现在';安全规则,swift,firebase,firebase-security,Swift,Firebase,Firebase Security,我在比较firebase db中保存的时间间隔与“现在”firebase安全规则时遇到问题。我写的规则是为了防止用户在一段时间后阅读帖子。 这就是我在数据库中保存时间戳的方式: "time": NSDate().timeIntervalSince1970 var followingPosts = [Post]() func loadUserFeed(_ update: @escaping () -> Void) { userFeedHandle = CURRENT_US

我在比较firebase db中保存的时间间隔与“现在”firebase安全规则时遇到问题。我写的规则是为了防止用户在一段时间后阅读帖子。 这就是我在数据库中保存时间戳的方式:

"time":  NSDate().timeIntervalSince1970
    var followingPosts = [Post]()
func loadUserFeed(_ update: @escaping () -> Void) {
    userFeedHandle = CURRENT_USER_FEED_REF.observe(DataEventType.value, with: {(snapshot) in
        self.followingPosts.removeAll()
        for child in snapshot.children.allObjects as! [DataSnapshot] {
            let post = Post(postID: child.key, postData: child.value as! Dictionary<String, Any>)
            self.followingPosts.append(post)
            self.followingPosts.sort(by: {  Double(truncating: $0.time) >  Double(truncating: $1.time)})
            update()
        }
        if snapshot.childrenCount == 0 {
            update()
        }

    })

}
这是我正在编写的安全规则,如果在某个时间之后出现以下情况,则应防止读取:

"follower-feed": {
  // ".read": "auth !== null",
  ".write": "auth !== null",
    "$userID": {
    "$postID": {
      ".read": "auth !== null && data.child('time').val() >= ((now / 1000) - 86400)",}}},
这是我用来存储帖子的数据库模式:

-从动件进给:{

用户\u uid\u 1:{

post_uid_1: {

     "time": 1515435031.16646

post_uid_2: {

     "time": 1515435091.22323
我想指出的是,我已经考虑到了“now”是以毫秒为单位的事实,并且将其除以1000应该会将我的两个数字设置为相同的秒时间值。我已经扼杀了所有关于firebase文档的内容,但没有任何东西可以帮助我解决这个问题。当我运行模拟器测试以确定请求的读取是否会通过时,我t表示它将通过。但是,在我的应用程序中,没有数据被读取

这是尝试从firebase读取数据的代码:

"time":  NSDate().timeIntervalSince1970
    var followingPosts = [Post]()
func loadUserFeed(_ update: @escaping () -> Void) {
    userFeedHandle = CURRENT_USER_FEED_REF.observe(DataEventType.value, with: {(snapshot) in
        self.followingPosts.removeAll()
        for child in snapshot.children.allObjects as! [DataSnapshot] {
            let post = Post(postID: child.key, postData: child.value as! Dictionary<String, Any>)
            self.followingPosts.append(post)
            self.followingPosts.sort(by: {  Double(truncating: $0.time) >  Double(truncating: $1.time)})
            update()
        }
        if snapshot.childrenCount == 0 {
            update()
        }

    })

}
var followPosts=[Post]()
func loadUserFeed(update:@escaping()->Void){
userFeedHandle=CURRENT_USER_FEED_REF.observe(DataEventType.value),在
self.followingPosts.removeAll()
snapshot.children.allObjects中的子对象为![DataSnapshot]{
让post=post(postd:child.key,postData:child.value as!Dictionary)
self.followingPosts.append(post)
self.followingPosts.sort(按:{Double(截断:$0.time)>Double(截断:$1.time)})
更新()
}
如果snapshot.childrenCount==0{
更新()
}
})
}

看来,
当前用户\u FEED\u REF
是包含给定用户帖子的位置,即
follower FEED/$userID
,您希望帖子年龄的安全规则充当过滤器,允许查询返回用户最近的帖子,并排除旧帖子。但安全规则是不是筛选器。对于任何位置,您都可以读取所有数据(包括其子项),也可以不读取任何数据。您没有允许在
follower feed/$userID
处读取的规则,因此在该位置的查询将失败


查看Firebase团队成员关于如何实现所需内容的想法,或搜索“Firebase规则不是筛选器”以查看其他相关问题和答案。

如果您取消对规则的注释
。请阅读:“auth!==null”
数据读取成功吗?是的,如果我将规则放在节点顶部,它读取成功。您可以发布尝试读取数据库但失败的代码吗?当我请求读取
/follower feed/some userID/some postd
时,该规则是正确的,适用于我使用Android客户端。请注意,规则不是过滤器。您没有关于位置的规则
follower feed
follower feed/$userID
,因此如果您在这些位置请求查询,它将失败。当然,我更新了我的帖子。感谢您澄清他们定义的过滤器。这就是我遇到的问题,我认为添加查询开始和结束可能会对我更公平。谢谢