简体   繁体   中英

reading data on firebase

This is my data structure. 在此处输入图片说明

This is how I load club data and its address.

func loadClubs() {

        ref = Database.database().reference()
        ref.child("club").observe(DataEventType.childAdded, with: { (clubSnapshot) in
            if let clubDict = clubSnapshot.value as? [String : AnyObject] {
                let name = clubDict["name"] as! String
                let explanation = clubDict["explanation"] as! String
                let courtNum = clubDict["courtNum"] as! Int
                let membershipFee = clubDict["membershipFee"] as! Int
                let visitorFee = clubDict["visitorFee"] as! Int
                let hasCarParking = clubDict["hasCarParking"] as! Bool
                let club2 = Club2(name: name, explanation: explanation, courtNum: courtNum, membershipFee: membershipFee, visitorFee: visitorFee, hasCarParking: hasCarParking)
                self.club2Array.append(club2) // Add to clubArray
                print(self.club2Array)
                self.tableView.reloadData()
            }
            let addressRef = Database.database().reference()
            addressRef.child("address").child(clubSnapshot.key).observe(DataEventType.childAdded, with: { (addressSnapshot) in
                if let addressDict = addressSnapshot.value as? [String: AnyObject] {

                    let clubAddress = ClubAddress(postCode: addressDict["postcode"] as! String, cityName: addressDict["city"] as! String, ward: addressDict["ward"] as! String, address1: addressDict["address1"] as! String, address2: addressDict["address2"] as! String)

                    self.addressArray.append(clubAddress)
                    print(self.addressArray)
                }
            })
        })
    }

basically, after retrieving each snapshot of club, I get club's key (-KsJB9TkoGNIkiZFGg7), then use that key to retrieve address. However, print(self.addressArray) doesn't not print anything. I add a debug breakpoint at if let addressDict = addressSnapshot.value as? [String: AnyObject] { if let addressDict = addressSnapshot.value as? [String: AnyObject] { , it does not stop the debug process. Why is it not calling? What do I miss here?

Ah! Your code is very close.

Keep in mind that .childAdded iterates over all of the child nodes and loads each one.

In your case, you don't want to iterate over all of the address nodes, you just want one, and don't want to leave an observer.

To do that, we load the specific node child data of the address node, by observeSingleEvent(of: .value. Here's a snippet of the important part.

let addressRef = Database.database().reference()
addressRef.child("address").child(clubSnapshot.key)
          .observeSingleEvent(of: .value, with: { (addressSnapshot) in
    let dict = addressSnapshot.value as! [String: Any]
    let address = dict["address1"] as! String
    print(address)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM