因此,我尝试将所有聊天室都设置为一个CollectionViewController,但我只能看到其中一个,而看不到我命名为聊天室的图像和标签。我所看到的只是一张白色图片和原始标签。我的代码:
func CreateNewRoom(user: User, caption: String, data: NSData) {
let filePath = "\(user.uid)/\(Int(NSDate.timeIntervalSinceReferenceDate))"
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.child(filePath).putData(data as Data, metadata: metaData) { (metadata, error) in
if let error = error {
print("Error uploading: \(error as NSError)")
return
}
let chatRef = self.storageRef.child("images/chat.jpg")
chatRef.downloadURL { url, error in
if error != nil {
} else {
}
}
if Auth.auth().currentUser != nil {
let idRoom = self.BASE_REF.child("rooms").childByAutoId()
idRoom.setValue(["caption": caption, "thumbnailUrlFromStorage": self.storageRef.child(metadata!.path!).description, "fileUrl": self.fileUrl])
}
}
}
func fetchDataFromServer(callback: @escaping (Room) -> ()) {
DataService.dataService.ROOM_REF.observeSingleEvent(of: .childAdded) { (snapshot) in
print("FOUND ROOM "+snapshot.key)
let room = Room(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callback(room)
}
}
答案 0 :(得分:0)
您只能看到一个聊天室的原因是因为您正在呼叫observeSingleEventv
,它将在第一个子节点之后停止监听。要从ROOM_REF
获取所有子节点,请使用observe
。
所以:
func fetchDataFromServer(callback: @escaping (Room) -> ()) {
DataService.dataService.ROOM_REF.observe(.childAdded) { (snapshot) in
print("FOUND ROOM "+snapshot.key)
let room = Room(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callback(room)
}
}