此功能成功从Firebase获取房间值并将其设置为var usersRooms: [Room] = []
:
userRoomRef.observeSingleEvent(of: .value, with: { (roomSnap) in
for eachRoom in roomSnap.value as! [String : AnyObject] {
let roomID = eachRoom.key as! String
userRoomRef.child(roomID).observeSingleEvent(of: .value, with: { (roomSnap) in
if let roomInfo = roomSnap.value as? [String : AnyObject] {
guard let roomName = roomInfo["roomName"] as! String! else {print("Error getting room name"); return}
guard let participants = roomInfo["participants"] as! [String]! else {print("Error getting room participants"); return}
print("\n\nRoom Name: \(roomName)\nRoom Participants: \(participants)\nRoom ID: \(roomID)\n")
self.usersRooms.append(Room(id: roomID, name: roomName, participants: participants)) // This array gets updated
self.currentUser?.rooms.append(Room(id: roomID, name: roomName, participants: participants))// This doesn't. Why?
print("\n\nUser's Rooms: \(self.usersRooms)\n\n")
print("\n\nRooms (from currentUser): \(self.currentUser?.rooms)\n\n")
}
})
}
})
打印:
Room Name: Room One
Room Participants: ["tzfHgGKWLEPzPU9GvkO4XE1QKy53"]
Room ID: -KhNIakMWNn1aKegRMZu
Room Name: Room Three
Room Participants: ["tzfHgGKWLEPzPU9GvkO4XE1QKy53"]
Room ID: -KhShqdKkOsfEsi_q7pE
Room Name: Room Two
Room Participants: ["tzfHgGKWLEPzPU9GvkO4XE1QKy53"]
Room ID: -KhNItkG77w0lLBo4Y0A
打印usersRooms
告诉我:
User's Rooms: [*app name*.Room, *app name*.Room, *app name*.Room]
但是当我尝试使用
填充表格视图时cell.textLabel?.text = usersRooms[(indexPath as NSIndexPath).row].name
空白。我错过了一步吗?似乎房间插入到数组中,我还使用usersRooms
的其他表视图方法。我在这里缺少什么?