首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Firebase数据库中获取数据?

从Firebase数据库中获取数据?
EN

Stack Overflow用户
提问于 2019-04-07 04:20:00
回答 1查看 38关注 0票数 0

我有这段代码,我想从FirebaseDatase加载一些数据

代码语言:javascript
复制
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数据库的结构没有新的field.is这是个问题吗?以下是Vinyl.swift的代码:

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-07 19:03:44

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

替换

代码语言:javascript
复制
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 }

使用

代码语言:javascript
复制
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 ?? ""

在处理async调用时,您还需要更新main queue中的UI,如下所示:

代码语言:javascript
复制
DispatchQueue.main.async {
    self.tableView.reloadData()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55553129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档