从Firbase数据库中获取数据

时间:2019-04-06 20:20:00

标签: swift database firebase

我希望你们能帮助我。 我有这段代码,我想从FirebaseDatase加载一些数据

    func fetchVinylsData(){
    SVProgressHUD.show()
    guard let currentUID = Auth.auth().currentUser?.uid else { return }

    dbRef.child("vinyls").child(currentUID).observe(.childAdded) { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String,AnyObject> else { return }
        let vinyl = Vinyl(dictionary: dictionary)
        print(dictionary)
        self.vinyls = [Vinyl]()
        self.vinyls.append(vinyl)
        self.vinyls.sort(by: { (vinyl1, vinyl2) -> Bool in
            return vinyl1.artist < vinyl2.artist
        })
        self.tableView.reloadData()
    }
    SVProgressHUD.dismiss()
}

当我打印字典时,我得到的所有记录都很好,但是我的表视图却没有被填充。直到我决定在Vinyl.swift中添加一个新字段之前,我一直工作得很好。 firebase数据库的结构没有新字段。这是问题吗? 这是Vinyl.swfit的代码:

import UIKit
import Firebase

class Vinyl {
var vinylID : String!
var title : String!
var artist : String!
var label : String!
var vinylCountry : String!
var isOut : Bool = false
var vinylLocation : String!
var year : String!
var vinylAutoID : String!
var vinylFormat : String!

init(dictionary : Dictionary<String,AnyObject>) {
    guard let title = dictionary["title"] as? String else { return }
    guard let artist = dictionary["artist"] as? String else { return }
    guard let label = dictionary["label"] as? String else { return }
    guard let vinylID = dictionary["vinylID"] as? String else { return }
    guard let vinylCountry = dictionary["vinylCountry"] as? String else { return }
    guard let vinylLocation = dictionary["vinylLocation"] as? String else { return }
    guard let year = dictionary["vinylYear"] as? String else { return }
    guard let autoID = dictionary["autoID"] as? String else { return }
    guard let isOut = dictionary["isOut"] as? Bool else { return }
    guard let vinylFormat = dictionary["format"] as? String else { return }

    self.vinylID = vinylID
    self.title = title
    self.artist = artist
    self.label = label
    self.vinylCountry = vinylCountry
    self.isOut = isOut
    self.vinylLocation = vinylLocation
    self.year = year
    self.vinylAutoID = autoID
    self.vinylFormat = vinylFormat
}

}

1 个答案:

答案 0 :(得分:0)

我不建议在您的guard let方法中使用init,因为假设dictionary["title"]为空或不可用,那么它将从代码的第一行返回,并且同样会发生其他。要解决此问题,您可以使用Nil-Coalescing Operator(意味着您可以分配默认值nilsomething["something"]niloptional的情况下分配其他任何值)

替换

guard let title = dictionary["title"] as? String else { return }
guard let artist = dictionary["artist"] as? String else { return }
guard let label = dictionary["label"] as? String else { return }
guard let vinylID = dictionary["vinylID"] as? String else { return }
guard let vinylCountry = dictionary["vinylCountry"] as? String else { return }
guard let vinylLocation = dictionary["vinylLocation"] as? String else { return }
guard let year = dictionary["vinylYear"] as? String else { return }
guard let autoID = dictionary["autoID"] as? String else { return }
guard let isOut = dictionary["isOut"] as? Bool else { return }
guard let vinylFormat = dictionary["format"] as? String else { return }

let title = dictionary["title"] as? String ?? ""
let artist = dictionary["artist"] as? String ?? ""
let label = dictionary["label"] as? String ?? ""
let vinylID = dictionary["vinylID"] as? String ?? ""
let vinylCountry = dictionary["vinylCountry"] as? String ?? ""
let vinylLocation = dictionary["vinylLocation"] as? String ?? ""
let year = dictionary["vinylYear"] as? String ?? ""
let autoID = dictionary["autoID"] as? String ?? ""
let isOut = dictionary["isOut"] as? Bool ?? false
let vinylFormat = dictionary["format"] as? String ?? ""

在处理如下所示的UI调用时,您还需要在主队列中更新async

DispatchQueue.main.async {
    self.tableView.reloadData()
}