firebase - Swift 3: FirebaseDB returning nil when child is present -
i have linked firebase app. when i'm trying read data db, data present in snapshot. when child read, returns nil.
here's code:
func checkforduplicatescan(qrcode: string) { dataservice.ds.ref_samples.observesingleevent(of: .value, with: { (snapshot) in if let dict = snapshot.value as? [string:any] { print(dict) print(qrcode) print(dict["\(qrcode)"]) if let sampledict = dict[qrcode] as? [string:any] { print(sampledict) if let isscanned = sampledict["scanned"] as? bool { if isscanned == true { print("already scanned") let alert = uialertcontroller(title: "already redeemed", message: "this offer has been redeemed you. stay tuned.", preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "ok", style: uialertactionstyle.default, handler: { (alert) in self.tabbarcontroller?.selectedindex = 0 })) self.present(alert, animated: true, completion: nil) } else { print("new scan") self.updateqrcode(qrcode: qrcode) } } else { print("error: can't read/find 'scanned' ") } }else { print("error: invalid code scanned") let alert = uialertcontroller(title: "invalid qr code scanned", message: "the code you've scanned invalid.", preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "ok", style: .default, handler: { (alert) in self.tabbarcontroller?.selectedindex = 0 })) self.present(alert, animated: true, completion: nil) } } else { print("error: can't dictionary snapshot value") } }) }
here's console log:
the log:
the dictionary came printing dict.
print(dict)the 'test13' came from
print(qrcode)the 'nil' came from
print(dict["\(qrcode)"])
this code working of yesterday has failed today.
help me out!!
edit: data i'm trying read.
here's json file json file
update: looks i've found out problem here.
when run code, nothing printed.
if let newdict = dict[qrcode] as? nsdictionary { print(newdict) }
but, when this, dict accessible.
if let newdict = dict["test10"] as? nsdictionary { print(newdict) }
note qrcode string having value "test10"
wierd asf!! still can't figure reason behind , how rectify this.
just check, problem seems data type , not firebase data. hence, getting "error: invalid code scanned". go like:
if let value = snapshot.value as? nsdictionary { let username = value?["username"] as? string ?? "" etc ... }
[string:any] has caused problems me in past. anyhow, helpful provide sample of data you're trying read.
Comments
Post a Comment