Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

swift - using variables outside of completion block

Im currently retrieving data from firebase the data is put inside an NSObject and then a completion block. The item inside of the completion block is store as a variable userBinfos. Variable userBinfos only work inside of the completion block i want to use this outside of the completion

var userBinfos = userObject()
    override func viewDidLoad() {
        super.viewDidLoad()

        userBinfo { (user) in
        self.userBinfos = user

        }  
//I want to use to variable here but it does not work 
        print(self.userBinfos.email)

    }


    func userBinfo(completion: (userObject) -> ()) {

        let dbFir = FIRDatabase.database().reference()

        let firRef = dbFir.child("frontEnd/users/(userId)")

        firRef.observeEventType(.Value, withBlock: { snapshot in

            let userDict = snapshot.value as! [String: AnyObject]
            self.name.text = userDict["firstname"] as? String
            self.userBio.text = userDict["userBio"] as! String

            var user = userObject()

            user.firstName = userDict["firstname"]
            user.lastName = userDict["lastname"]
            user.email = userDict["email"]
            user.profileImageUrl = userDict["profileImageUrl"]
            user.userBio = userDict["firstname"]
            user.userId = userDict["firstname"]

                   dispatch_async(dispatch_get_main_queue(), {
            completion(user)

            })

        }) { (error) in
            print(error)
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...