Firebase Retrieving Data in Swift

后端 未结 6 2176
长情又很酷
长情又很酷 2020-12-14 12:35

I\'m trying to retrieve specific data from just the currently logged in user. My data in my database looks like this:

相关标签:
6条回答
  • 2020-12-14 12:53
        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
    
                                  }
    
                               
                        }
    
    
                        })
                        
    
    0 讨论(0)
  • 2020-12-14 12:55
    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)")
    
        }
    
    0 讨论(0)
  • 2020-12-14 12:57

    Swift 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!)
    
        })
    
    0 讨论(0)
  • 2020-12-14 13:01
    {
        "rules": {
             "tbl_name": {
                ".indexOn": ["field_name1", "field_name2"]
             },
        ".read": true,
        ".write": true
        }
    }
    

    You can apply indexOn on any field. Add this json in rules security and rules tab. Hope this works for you. :)

    0 讨论(0)
  • 2020-12-14 13:01

    It retrieves logged in user data:

    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)
    
    0 讨论(0)
  • 2020-12-14 13:03

    It gives you that warning message indexOn because you are doing a query.

    you should define the keys you will be indexing on via the .indexOn rule in your Security and Firebase Rules. While you are allowed to create these queries ad-hoc on the client, you will see greatly improved performance when using .indexOn

    As you know the name you are looking for you can directly go to that node, without a query.

        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
        })
    
    0 讨论(0)
提交回复
热议问题