【问题标题】:Swift 3 and Firebase Observe Child if it existSwift 3 和 Firebase 观察孩子(如果存在)
【发布时间】:2018-04-23 09:40:17
【问题描述】:

需要观察我的 Firebase 数据库 中的节点(如果存在)。

我使用的代码是这样的:

 var COLOR_REF = Database.database().reference().child("Colors")

self.COLOR_REF.child(id).observe(.childAdded) { (snapshot) in
                let key = snapshot.key
                Api.Post.getMyPosts(withId: key, completion: { (post) in
                    completion(post)
                })
            }

id 可能存在也可能不存在... 我需要的是进行观察

  if child(id) exist else do something else.

有没有办法在一次观察中做到这一点,或者我需要观察COLOR_REF,然后如果 id 存在观察孩子?

谢谢! - - - - - - - - - - - - - - - 更新 根据图片可以说我的 id 是:

D7CGNLehdjksnjkdsiwgqauymj3 我像这样进行观察:

COLOR_REF.child(id).observe(.childAdded) { (snapshot) in
if snapshot.exists() {
print("YES")
} else {
print("No")
}
 let key = snapshot.key
  Api.Post.getMyPosts(withId: key, completion: { (post) in
  completion(post)
     })
   }

因此假设 id 在我执行观察时控制台既不打印“YES”也不打印“NO”。所以基本上它似乎永远不会进入观察者

------------------第二次更新 这是我现在的做法:

func observeFeed(withUserId id: String, completion: @escaping (Post) -> Void, isEmpty: @escaping () -> Void) {
    COLORS_REF.observe(.value) { (snapshot) in
        if snapshot.hasChild(id) {
            self.COLORS_REF.child(id).observe(.childAdded) { (snapshot) in
                let key = snapshot.key
                Api.Post.getMyPosts(withId: key, completion: { (post) in
                    completion(post)
                })
            }
        } else {
            isEmpty()
        }
    }
}

就像这样工作......我确实在完成后将所有内容存储在一个数组中。我的问题是,如果这是用户第一次登录该部分数据库将是空的。 所以我想知道是否有一种方法可以实现这一点而不必使用双重观察者......

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    您可以通过检查 snapshot.exists() 在这样的观察上是否返回 true 或 false 来检查 id 是否存在:

        self.COLOR_REF.child(id).observe(.childAdded) { (snapshot) in
                if snapshot.exists() {
                   //the id is valid
                }
                else {
                    //the id is not there. do something else
                 }
    
        }) { (error) in
    
            print(error.localizedDescription)
    
            //snapshot might have been nil, or there was no id or security rules denied permission to read. do something else.
    
        }
    

    【讨论】:

    • 我试过这样...但是如果 id 不存在 id 不会给我任何快照..
    • 它应该为 snapshot.exists() 返回 true 或 false。如果没有,请尝试在 Firebase 观察后添加 cancelBlock。我已经更新了我的答案以显示这一点。如果快照返回 nil,它应该进入取消块并且你知道没有 id。
    • 它永远不会进入cancelBlock,因为实际上没有错误...问题是它永远不会触发观察者,因为child(id)不存在...
    • @Marco 你发现如何知道孩子是否存在??
    【解决方案2】:

    你问的是这样的吗? Firebase 本身提供了一个默认函数来在处理程序执行后检查快照是否为空?

    self.COLOR_REF.child(id).observe(.childAdded) { (snapshot) in
    
    if snapshot.exists(){
        //id exists
        let key = snapshot.key
        Api.Post.getMyPosts(withId: key, completion: { (post) in
            completion(post)
        })
    }
    else{
        //id do not exists
    }
    
    }
    

    重新更新 - 回答

    如你想检查数据库中是否存在id,可以通过两种方式管理

    1) 当您加载应用程序或任何地方并使用该数组进行检查时,在数组中离线获取所有 id 编号 2) 在满足第一个处理程序的情况下,需要使用两个处理程序进入第二个处理程序

    需要代码,我将为第二个选项设计该代码

    更新答案 -

    如何在数据库中将 isDatabaseEmpty 的值管理为 Bool 或字符串

    Sp DB 要像

    ----Root
      ---UserID
        --isDatabaseEmpty : "True" // if first time user and "False" //if db has values
          --Colors
            -Value1
            -Value2
    
      ---UserID
        --isDatabaseEmpty : "True" // if first time user and "False" //if db has values
          --Colors
            -Value1
            -Value2
    

    现在检查 isDatabaseEmpty if False Go to second handler else no, 会有帮助吗?

    【讨论】:

    • 这就是我正在尝试的......但如果数据库中不存在该id,它似乎没有快照......
    • 这就是你想要的,如果在处理程序执行后如果快照结果为 nil,这意味着你查看的 id 不在数据库中?或者我弄错了你正在寻找的其他东西?
    • 如果 id 不存在显然不会返回快照值,您需要用它管理 tableView 或 collectionView ,是的,如果您需要数据而 id 不存在,您可以在其他情况下传递一些静态数据
    • 不,那将是完美的......但如果孩子不在那里,它似乎根本不会进入观察......如果我这样做:COLORS_REF.child(id).observe(. childAdded) { (snapshot) in print("Here")
    • 我认为问题出在 .childAdded ... 如果我使用 value 它适用于 if snapshot.exists()。问题是我需要观察何时添加颜色...
    【解决方案3】:

    我能解决的唯一方法是这样的:

    func observeFeed(withUserId id: String, completion: @escaping (Post) -> Void, isEmpty: @escaping () -> Void) {
        COLOR_REF.observeSingleEvent(of: .value) { (snapshot) in
            if !snapshot.hasChild(id) {
                isEmpty()
                return
            }
        }
       COLOR_REF.child(id).observe(.childAdded) { (snapshot) in
            let key = snapshot.key
            Api.Post.getMyPosts(withId: key, completion: { (post) in
                completion(post)
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 2018-01-30
      相关资源
      最近更新 更多