简体   繁体   中英

Cannot retrieve data from Firebase using Swift

As the title says I have a weird problem to retrieve simple data from Firebase, but I really can't figure out where I'd go wrong.

This is my schema:

在此处输入图片说明

And this the code:

import Firebase

let DB_BASE = Database.database().reference()

class FirebaseService {

    static let instance = FirebaseService()

    private var REF_BASE = DB_BASE
    private var REF_SERVICE_STATUS = DB_BASE.child("Service_Status")

    struct ServiceStatus {
        var downloadStatus: Bool
        var uploadStatus: Bool
    }

    func getServiceStatus() -> (ServiceStatus?) {
        var serviceStatus: ServiceStatus?

        REF_SERVICE_STATUS.observeSingleEvent(of: .value) { (requestSnapshot) in

            if let unwrapped = requestSnapshot.children.allObjects as? [DataSnapshot] {
                for status in unwrapped {
                    serviceStatus.downloadStatus = status.childSnapshot(forPath: "Download_Status").value as! Bool
                    serviceStatus.uploadStatus = status.childSnapshot(forPath: "Upload_Status").value as! Bool
                }
                // THANKS TO JAY FOR CORRECTION
                return sponsorStatus
            }
        }
    }
}

but at the end serviceStatus is nil. Any advice?

I think you may be able to simplify your code a bit to make it more manageable. Try this

let ssRef = DB_BASE.child("Service_Status")
ssRef.observeSingleEvent(of: .value) { snapshot in
    let dict = snapshot.value as! [String: Any]
    let down = dict["Download_Status"] ?? false
    let up = dict["Upload_Status"] ?? false
}

the ?? will give the down and up vars a default value of false if the nodes are nil (ie don't exist)

Oh - and trying to return data from a Firebase asynchronous call (closure) isn't really going to work (as is).

Remember that normal functions propagate through code synchronously and then return a value to the calling function and that calling function then proceeds to the next line of code.

As soon as you call your Firebase function, your code is going to happily move on to the next line before Firebase has a chance to get the data from the server and populate the return var. In other words - don't do it.

There are always alternatives so check this link out

Run code only after asynchronous function finishes executing

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