我做我的#2搜索,但我沒有找到我要找的如何在數據庫中搜索火力地堡
的解決方案,我想在數據庫火力點進行搜索,如果「名稱」等於在名稱導航標題,並在「電子郵件」該用戶使用,以簽到等於在數據庫中的電子郵件,然後它會避免用戶添加新項
JSON的看起來像這樣
"Items" : {
"-KUMSKLFqMjclbqygnPL" : {
"Item" : "asdfg"
"Name" : "Fadi」,
"User Email" : "[email protected]"
}
}
我的代碼是
let databaseRef = FIRDatabase.database().reference()
var NAME : String!
var UserEmail : String!
var email : String!
databaseRef.child("Items").queryOrderedByKey().observe(.childAdded, with: { snapshot in
let snapshotValue = snapshot.value as? NSDictionary
NAME = (snapshotValue?["Name"] as? String)!
UserEmail = (snapshotValue?["User Email"] as? String)!
email = (FIRAuth.auth()?.currentUser?.email)!
if NAME == navigationItem.title {
if UserEmail == email {
print ("you can't add new item \(UserEmail)")
}else if UserEmail != email {
print("you can add new Item")
}
}
})
我的問題是如果存在名稱相同的Fadi但有兩個不同的電子郵件 其結果將是
you can't add new item Optional("[email protected]")
you can add new Item
應該只打印
you can't add new item Optional("[email protected]")
我不知道如何打破它現在看起來像循環所有數據庫
更新:
我嘗試德拉威答案
結果打印(snapShot.value):
"-KUMS7J-rCglHrVGX840" = {
"Item" = "asdsd"
"Name" = "a";
"User Email" = "[email protected]";
};
結果是正確的!但現在我需要從該節點提取用戶電子郵件
您已經表明您使用的是斯威夫特通過'快速'標籤。無需將其添加到標題中。 –