I have this code that I want to load some data from 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()
}
when I print dictionary I get all the records fine but my tableview does not get populated.I was working fine until I decided to add a new field to Vinyl.swift. The structure at firebase database is without the new field.is this a problem? Here is the code for 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
}
}
I would not suggest to use guard let
in your init
method because suppose dictionary["title"]
is empty or not available then it will return from the first line of your code and same will happen for others. And to solve this issue you can use Nil-Coalescing Operator
(Means you can assign default value nil
or any other value if something["something"]
is nil
or optional
)
Replace
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 }
with
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 ?? ""
And you also need to update your UI
in main queue when you are working with async
calls like shown below:
DispatchQueue.main.async {
self.tableView.reloadData()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.