簡體   English   中英

Firebase 在 Swift 中檢索數據

[英]Firebase Retrieving Data in Swift

我正在嘗試僅從當前登錄的用戶中檢索特定數據。 我的數據庫中的數據如下所示:

在此處輸入圖片說明

例如,我只想獲取 full_name 並將其保存在變量 userName 中。 以下是我用來獲取數據的內容

ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
            print(snapshot.value)
            // let userName = snapshot.value["full_name"] as! String
        })

不幸的是,這是我的控制台打印的內容。

在此處輸入圖片說明

我將不勝感激:) 謝謝!

它為您提供警告消息indexOn因為您正在執行查詢。

您應該通過安全和 Firebase 規則中的 .indexOn 規則定義您將索引的鍵。 雖然您可以在客戶端上臨時創建這些查詢,但使用 .indexOn 時,您會看到性能大大提高

正如您知道要查找的名稱一樣,您可以直接轉到該節點,無需查詢。

    let ref:FIRDatabaseReference! // your ref ie. root.child("users").child("stephenwarren001@yahoo.com")

    // only need to fetch once so use single event

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

        if !snapshot.exists() { return }

        //print(snapshot)

        if let userName = snapshot.value["full_name"] as? String {
            print(userName)
        }
        if let email = snapshot.value["email"] as? String {
            print(email)
        }

        // can also use
        // snapshot.childSnapshotForPath("full_name").value as! String
    })

斯威夫特 4

let ref = Database.database().reference(withPath: "user")
    ref.observeSingleEvent(of: .value, with: { snapshot in

        if !snapshot.exists() { return }

        print(snapshot) // Its print all values including Snap (User)

        print(snapshot.value!)

        let username = snapshot.childSnapshot(forPath: "full_name").value
        print(username!)

    })
{
    "rules": {
         "tbl_name": {
            ".indexOn": ["field_name1", "field_name2"]
         },
    ".read": true,
    ".write": true
    }
}

您可以在任何字段上應用 indexOn。 在規則安全和規則選項卡中添加此 json。 希望這對你有用。 :)

它檢索登錄的用戶數據:

let ref = FIRDatabase.database().reference(fromURL: "DATABASE_URl")
let userID = FIRAuth.auth()?.currentUser?.uid
let usersRef = ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)
let ref = Database.database().reference().child("users/stephenwarren001@yahoo.com")


    ref.observeSingleEvent(of: .value, with: { (snap : DataSnapshot)  in

        print("\(String(describing:  snap.value))")


    }) { (err: Error) in


        print("\(err.localizedDescription)")

    }
    var refDatabase = DatabaseReference()

    refDatabase = Database.database().reference().child("users");
 let refUserIdChild = refDatabase.child("stephenwarren001@yahoo.com")
                    
                    refUserIdChild.observeSingleEvent(of: .value, with: { snapshot in

                        if !snapshot.exists() { return }

                        print(snapshot) // Its print all values including Snap (User)

                        print(snapshot.value!)
                        

                    if let tempDic : Dictionary = snapshot.value as? Dictionary<String,Any>
                    {
                           
                            if let userName = tempDic["full_name"] as? String {
                                  self.tfName.text = userName

                              }

                            if let email = tempDic["email"] as? String {
                                    self.tfEmail.text  = email

                              }

                           
                    }


                    })
                    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM