gpt4 book ai didi

swift - 从 Firebase 数据库获取数据?

转载 作者:行者123 更新时间:2023-11-30 10:47:03 25 4
gpt4 key购买 nike

我有这段代码,我想从 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()
}

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

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
}
}

最佳答案

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

替换

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,如下所示:

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

关于swift - 从 Firebase 数据库获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com