我想创建一个Firebase实例数组,每个实例都有侦听器触发相同的例程。
例如:假设数据存储在root中:
https://someapp.firebaseio.com/chatrooms/RoomFoo/John/...
https://someapp.firebaseio.com/chatrooms/RoomFoo/Mary/...
..
..
https://someapp.firebaseio.com/chatrooms/RoomBar/Alice/...
https://someapp.firebaseio.com/chatrooms/RoomBar/Bob/...
每个房间内的成员都为该房间的每个用户都有一个监听器(childAdded和childRemoved)
让我们说Adam加入RoomFoo,会发生以下情况:
类似地:
如果我构建一个Firebase引用数组,我可以添加/删除对此数组的引用并设置侦听器,但这些侦听器永远不会被调用!
这就是声明数组的方式(Swift)
var allUserRef : [Firebase] = []
附加对此数组的引用并设置侦听器的逻辑
func userJoinedRoom(snapshot: FDataSnapshot!) {
print(snapshot.key + " Joined!")
let firebaseRootRef = Firebase(url: firebaseRootUrl)
let roomsRootRef = firebaseRootRef.childByAppendingPath(roomsRelativePath)
let userRoomRef = roomsRootRef.childByAppendingPath(snapshot.key)
//read initial data as well : TODO
allUserRef.append(userRoomRef)
print(allUserRef) //Works great - i can see a list of references
userRoomRef.observeEventType(.ChildAdded, withBlock: userAddedData)
userRoomRef.observeEventType(.ChildRemoved, withBlock: userRemovedData)
}
问题是永远不会调用userAddedData和userRemovedData
我的错误是什么?
答案 0 :(得分:0)
我通过维护字典中的句柄(由userid索引)而不是像Jay在评论中指出的Firebase实例来解决我的问题