如何将Firebase异步请求中的数据保存在全局变量中?

时间:2019-05-06 18:35:27

标签: ios swift firebase asynchronous firebase-realtime-database

我知道这已经在堆栈溢出中被问过几次了,但是我似乎找不到我要寻找的答案。我正在尝试将Firebase数据库中的数据(使用observeSingleEvent(snapshot))存储在全局变量中。有关详细信息,请参见下面的代码。

我尝试添加完成处理程序,并按照联机步骤操作,但是这样做,observeSingleEvent请求停止工作。

我以前有过:

class ItemsTableViewController: UITableViewController {

    let listToUsers = "ListToUsers"

    var user: User!
    let ref = Database.database().reference()

    override func viewDidLoad() {
        super.viewDidLoad()
        let defaultUser = User()
        ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            defaultUser.uid = uid
            defaultUser.email = value?["email"] as? String ?? ""
            defaultUser.name = value?["name"] as? String ?? ""
            defaultUser.grad = value?["grad"] as? Int ?? 0
            defaultUser.number = value?["number"] as? String ?? ""
            defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!
            completion(defaultUser)
        }) { (error) in
            print("hello")
            print(error.localizedDescription)
        }
        self.user = defaultUser
    }
}

我尝试过但仍然无法解决的问题:

class ItemsTableViewController: UITableViewController {

    // MARK: Constants
    let listToUsers = "ListToUsers"

    // MARK: Properties
    var user: User!
    let ref = Database.database().reference()

    override func viewDidLoad() {
        super.viewDidLoad()
        let use = Auth.auth().currentUser?.uid
        self.getUserData(uid:use!) { (user) -> () in
            self.user = user
        }
}

func getUserData(uid:String , completion: @escaping (User) -> ()) {
        ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
                let value = snapshot.value as? NSDictionary
                let defaultUser = User()
                defaultUser.uid = uid
                defaultUser.email = value?["email"] as? String ?? ""
                defaultUser.name = value?["name"] as? String ?? ""
                defaultUser.grad = value?["grad"] as? Int ?? 0
                defaultUser.number = value?["number"] as? String ?? ""
                defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!
                completion(defaultUser)
        }){ (error) in
            print("hello")
            print(error.localizedDescription)
        }
    }

1 个答案:

答案 0 :(得分:0)

Firebase是异步的,在Firebase有机会返回数据之前,第一个代码块中的self.user = defaultUser将被调用。

数据仅在Firebase调用后的闭包内有效。所以这是解决方法。哦,删除不需要的错误代码。

假设这样的用户结构

users
  uid_0
    name: "some name"
    email: "some email"

以及要在uid_0中读取并填充全局(即class var)用户的代码

class MyUser {
    var uid = ""
    var name = ""
    var email = ""

    init(aUid: String, aName: String, aEmail: String) {
        self.uid = aUid
        self.name = aName
        self.email = aEmail
    }
}

var user: MyUser!

func readOneUser() {
    let uid = "uid_0"
    let thisUserRef = self.ref.child("users").child(uid)
    thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
        //let uid = snapshot.key if you don't know the uid, it will be the key to the node
        let name = snapshot.childSnapshot(forPath: "name").value as? String ?? "No Name"
        let email = snapshot.childSnapshot(forPath: "email").value as? String ?? "No Email"
        self.user = MyUser(aUid: uid, aName: name, aEmail: email)
        print(self.user.name) //do something with the user here
    })
}

和这一行

defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!

需要解决,但我不知道目的是什么。如果发生这种情况,最终可能会变为零

defaultUser.image = UIImage(named: "")!

所以您可能要用某种默认图像填充它,或者在它为nil时提供错误处理。