I have linked Firebase to my app. When I'm trying to read data from the DB, data is present in the snapshot. But when a child is read, it returns nil.
Here's the 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 already been redeemed by 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 that you've scanned is 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 get dictionary from snapshot value")
}
})
}
Here's the console log:
The log:
The dictionary came from printing dict.
print(dict)The 'test13' came from
print(qrCode)The 'nil' came from
print(dict["\(qrCode)"])
This code was working as of yesterday but has failed today.
Help me out!!
Edit: This is the data that I'm trying to read.
Here's the JSON file as well JSON FILE
Update: Looks like I've found out the problem here.
When I run this code, nothing is printed.
if let newDict = dict[qrCode] as? NSDictionary {
print(newDict)
}
But, when I do this, the dict is accessible.
if let newDict = dict["test10"] as? NSDictionary {
print(newDict)
}
Note that qrCode is a string having value "test10"
Wierd asf!! Still can't figure the reason behind it and how to rectify this.