2017-07-02 108 views
0

我想檢查用戶名是否存在。Swift + Firebase - 檢查具有autoId的節點的值是否存在

但Xcode抱怨我的安全規則。

[Firebase/Database] [I-RDB034028]使用未指定的索引。考慮 加入「.indexOn」:‘個人資料/用戶名’AT /用戶您的安全規則 獲得更好的性能

如果我刪除‘$ UID’的Xcode不抱怨了,但我的腳本不工作得更好。

快照總是空,即使我進入一個存在於火力

用戶名我已經檢查了論壇,來自不同用戶想盡了各種辦法,但沒有固定我的問題,所以也許我做錯了什麼?

這是我的規則

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null", 
     "users": { 
     "$uid": { 
      "profile":{ 
      ".indexOn":["username"] 
      } 
     } 
     } 
    } 
} 

這是我的腳本

//check if username exist in the database 
    static func checkIfUsernameExist(username: String, callback: @escaping (_ found: Bool) -> Void){ 
     print(username) 
     let databaseRef = Database.database().reference(withPath: "users") 
     databaseRef.queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in 
      print(snapshot) 
      callback(snapshot.exists()) 
     }) 
    } 

enter image description here

回答

0

根據我有限的經驗,我databaseRef

databaseRef.child("users").queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in 
     print(snapshot) 
     callback(snapshot.exists()) 
    }) 
添加.child

至於.indexOn,一旦您的數據規模增大,它將會有所幫助。