gpt4 book ai didi

swift - 无法将数据库时间戳与 firebase 'now' 安全规则进行比较

转载 作者:行者123 更新时间:2023-11-28 15:08:00 25 4
gpt4 key购买 nike

我在将保存在我的 firebase 数据库中的时间间隔与“现在”的 firebase 安全规则进行比较时遇到问题。我正在编写的规则旨在防止用户在特定时间后阅读帖子。这就是我在我的数据库中保存时间戳的方式:

"time":  NSDate().timeIntervalSince1970

这是我正在编写的安全规则,如果在特定时间后应该阻止读取:

"follower-feed": {
// ".read": "auth !== null",
".write": "auth !== null",
"$userID": {
"$postID": {
".read": "auth !== null && data.child('time').val() >= ((now / 1000) - 86400)",}}},

这是我用来存储帖子的数据库架构:

-关注者供稿:{

user_uid_1:{

post_uid_1: {

"time": 1515435031.16646

post_uid_2: {

"time": 1515435091.22323

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

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

    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()
}

})

}

最佳答案

看起来 CURRENT_USER_FEED_REF 是包含给定用户帖子的位置,即 follower-feed/$userID,并且您期望帖子年龄的安全规则将充当过滤器,允许查询返回用户最近的帖子,并排除旧帖子。但安全规则不是过滤器。对于任何位置,您要么能够读取所有数据(包括其子数据),要么不能读取任何数据。您没有允许在 follower-feed/$userID 读取的规则,因此在该位置的查询将失败。

参见 this answer从 Firebase 团队成员那里获取有关如何实现所需内容的想法,或搜索“firebase 规则不是过滤器”以查看其他相关问题和答案。

关于swift - 无法将数据库时间戳与 firebase 'now' 安全规则进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com