Reading/Retrieving Data From Firebase

∥☆過路亽.° 提交于 2020-01-25 22:05:25

问题


I am a beginner to Swift 3 (using iOS 8.3). I have been trying to figure out a way to separate the data but got no luck. Basically, I am able to extract the following data from Firebase:

 {
Database =     {
    Kelvin =         {
        Institution = xxx;
        "Years of Experience" = "2.5";
        location = London;
    };
    Sophia =         {
        Institution = xxxx;
        "Years of Experience" = 3;
        location = London;
    };
};
Users =     {
    SOOlIFsjn3839jcmlBbVEnSRH3 =         {
        email = "testing@gmail.com";
        password = 1234567;
    };
};
       }

But now I want to separate the data so that it will show something like this:

        var ref: FIRDatabaseReference!
          ref = FIRDatabase.database().reference()
            ref.observe(FIRDataEventType.value, with: { (snapshot) in

            if let mydata = snapshot.value as? NSDictionary {
                print(mydata)

                 if let namedata = mydata["Database"] as? NSArray {
                    print(namedata[0])  //Prints Kelvin
                    print(namedata[1])  //Prints Sofia

                }

However, Swift doesn't allow me to do so, it tells me I cannot convert a type of NSDictionary to NSArray. Any ideas? Very much appreciated.


回答1:


Firebase collections are key-value pair. For each key of the JSON, you can find the corresponding value.

So you'll need to convert to an NSDictionary of String, Object. The keys in that dictionary will be Kelvin and Sophia. The value for each is the value under the corresponding key.




回答2:


READ/RETRIVING DATA FROM FIREBASE DATABASE to NSDICTIONARY

Read Data Every time it changes

You can use the FIRDataEventTypeValue event to read the data at a given path, as it exists at the time of the event. This method is triggered once when the listener is attached and again every time the data, including any children, changes. The event callback is passed a snapshot containing all data at that location, including child data. If there is no data, the value of the snapshot returned is nil.

/*FIREBASE VALUE_EVENT_LISTENER */
func getModelFromFirebase(){

    var firDatabaseReference: FIRDatabaseReference!
    firDatabaseReference = FIRDatabase.database().reference(withPath: "referencePath")
    firDatabaseReference.child("referenceKey").child(bookingId).observe(FIRDataEventType.value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        // Play with dict..

    }) { (error) in
        print(error.localizedDescription)
    }

}

Read Data Once

This is useful for data that only needs to be loaded once and isn't expected to change frequently or require active listening. For instance, the blogging app in the previous examples uses this method to load a user's profile when they begin authoring a new post.

/* FIREBASE SINGLE_VALUE_EVENT_LISTENER */
func getModelFromFirebase(){

    var firDatabaseReference: FIRDatabaseReference!
    firDatabaseReference = FIRDatabase.database().reference(withPath: "referencePath")
    firDatabaseReference.child("referenceKey").observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        // play with data Snapshot...

    }) { (error) in
        print(error.localizedDescription)
    }


}


来源:https://stackoverflow.com/questions/43858957/reading-retrieving-data-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!