我正在使用firebase来存储将在表格视图中显示的信息。这是数据库的屏幕截图:
我试图只添加来自某个“createdBy uid”的媒体:
func fetchMyMedia() {
let ref = DatabaseReference.media.reference()
let query = ref.queryOrdered(byChild: "createdBy/uid").queryEqual(toValue: "Cdxbj1iCZUNxWRbIBllBxKqXh5P2")
query.observe(.value,
with: { snapshot in
for childSnapshot in snapshot.children {
self.myMedia.insert(childSnapshot as! Media, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
}
})
}
“myMedia”是Media
类型的数组我收到错误,我无法将FIRDataSnapshot类型的值转换为XXXXXX.Media。对此的任何帮助将不胜感激。
如果它有帮助,没有列出我的整个Model类,这就是我如何得到所有未经过滤的信息......
extension Media {
class func observeNewMedia(_ completion: @escaping (Media) -> Void) {
DatabaseReference.media.reference().observe(.childAdded, with: { snapshot in
let media = Media(dictionary: snapshot.value as! [String : Any])
completion(media)
})
}
然后......
func fetchMedia() {
Media.observeNewMedia { (media) in
if !self.media.contains(media) {
self.media.insert(media, at: 0)
self.tableView.reloadData()
}
}
}
答案 0 :(得分:0)
func fetchMyMedia() {
let ref = DatabaseReference.media.reference()
let query = ref.queryOrdered(byChild: "createdBy/uid").queryEqual(toValue: "Cdxbj1iCZUNxWRbIBllBxKqXh5P2")
query.observe(.childAdded, with: { snapshot in
let media = Media(dictionary: snapshot.value as! [String : Any])
self.myMedia.insert(media, at: 0)
self.tableView.reloadData()