ios - Firebase .childAdded 不起作用,因为观察到的 child 可能还不存在

标签 ios swift firebase firebase-realtime-database

我想使用数据库中的一些数据以备后用。然而,我想要加载的数据可能还不存在,因为观察到的数据是 friend 请求的字典,在第一次使用应用程序时并不存在。虽然 .childAdded 观察者应该这样做,但它只是停在那里,代码不会进一步执行

func getFriendRequest(_ uid: String, completion: @escaping ([String]) -> Void) {
    var friendRequests = [String]()
    let currentUserRef = DatabaseReference.users(uid: self.currentUser.uid).reference()
    currentUserRef.child("sendFriendRequest").observe(.childAdded, with: { (snap) in
        let request = FriendRequest(dictionary: snap.value as! [String : Any])
        let requestedUid = request.uid
        print(requestedUid)
        friendRequests = [requestedUid]
        completion(friendRequests)
    }, withCancel: nil)
}

它没有进入观察方法...... 有人知道这是为什么吗?

最佳答案

.childAdded 完成处理程序只有在有 child 时才会被调用。如果没有子节点,它将不会被调用,直到您在 sendFriendRequest 下添加一个子节点。

关于ios - Firebase .childAdded 不起作用,因为观察到的 child 可能还不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730905/

相关文章:

javascript - 查询 Geofire 中该区域前 X 条记录?

ios - 从 Fabric 迁移到 Firebase 时,我应该更改 dsym 文件上传脚本吗?

ios - 为什么 Xcode 或 iOS 会用难看的角渲染我的图标?

ios - 如何在Swift中从观察对象中提取字符串值

ios - swift IOS : not all data is saved to firebase

swift - 从项目访问保存在今天扩展中的文件

ios - 放置 UIView 覆盖 TabBar 和 NavBar

ios - 今天小部件应用程序启动是 'over-sensitive'

javascript - Firebase 实时数据库安全规则 - 检查其他节点

ios - UIStoryboard实例化ViewControllerWithIdentifier : Can we call this in background thread?