如何等待Firebase状态更改



目前,我有一个Firebase实时数据库,其中有一个在线用户列表。

在我的iOS应用程序中,我有一个从数据库加载在线用户队列的功能。

当有0个用户在线时,我希望应用程序等待Firebase状态发生变化,用户上线,然后返回用户的id(user.id(。

我的数据库结构非常简单:

{
"online" : {
"user1" : val1,
"user2" : val2,
"user3" : val3
}
}

我正在尝试实现的功能:

let rtdbRef = Database.database().reference(withPath: "online")
userQueue: [String] = [] 
func nextUserInQueue() -> String {

// once we exhaust our queue, load up more users
if (self.userQueue.count == 0) {
// need to wait until a user goes online, then put them in the queue
}
}

基于我几年前写的这个要点,你可以等待一个节点有一个值:

var handle: UInt!
handle = rtdbRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("The value is now (snapshot.value)")
ref.removeObserverWithHandle(handle)
}
})

最新更新