且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Swift中使用异步Firebase方法实例化类?

更新时间:2022-05-01 23:00:17

这很hacky.

您应该在init方法中添加completionHandler.因此,当您的异步调用完成时,您将获得对象的实际值.

You should add completionHandler in init method. So, when your asynchronous call completed you will get actual value of object.

init(snapshot: DataSnapshot, completionHandler: @escaping (User) -> Void) {

    self.uid = snapshot.key

    getFirebasePictureURL(userId: uid) { (url) in

        self.getFirebaseNameString(userId: self.uid) { (fullName) in

            self.fullName = fullName
            self.profilePictureURL = url

            completionHandler(self)
        }
    }
}

我希望这会对您有所帮助.

I hope this will help you.