使用完成块之外的变量 [英] using variables outside of completion block

查看:145
本文介绍了使用完成块之外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从firebase中检索数据,数据放在 NSObject 中,然后是一个完成块。完成块内的项目被存储为一个变量 userBinfos 。变量 userBinfos 只能在完成块内部工作,我想在完成之外使用这个

  var userBinfos = userObject()
覆盖func viewDidLoad(){
super.viewDidLoad()

userBinfo {(user)in
self。 userBinfos = user

}
//我想在这里使用变量,但不起作用
print(self.userBinfos.email)

}

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

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

让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 u ser = 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_get_main_queue(),{
completion(user)
$ b})
$ b}){(error)in
print(error)


$ b $ / code


解决方案

userBinfo 完成参数的全部目的是提供一种机制,用于在异步<$调用c $ c> observeEventType 。因此,将代码视为在 userBinfo {user in ...} closure中异步方法的完成。

如果在调用异步完成闭包之前,部分UI没有任何意义, code> viewDidLoad 配置用户界面来显示(也许显示一个 UIActivityIndi​​catorView 或其他),然后删除完成处理程序内的东西。
$ b $ pre $ override func viewDidLoad(){
super.viewDidLoad()

//做任何你想让用户知道异步
//正在发生的事情,例如添加一个旋转的`UIActivityIndi​​catorView`或者其他任何

userBinfo {
中的用户self.userBinfos = user

//在这里更新UI,包括删除我们提供给让
//用户知道异步过程正在进行中。如果你是
//处理UITableView`,你可以在这里调用`tableView.reloadData()`。
}

//但这里不是
}


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)
        }

    }

解决方案

The entire purpose of the completion parameter of userBinfo is to provide a mechanism for being informed when the asynchronous observeEventType is called. So put code contingent upon the completion of that asynchronous method inside the userBinfo { user in ... } closure.

And if part of the UI doesn't make sense until that asynchronous completion closure is called, then have viewDidLoad configure the UI to make that explicit (perhaps show a UIActivityIndicatorView or whatever) and then remove that stuff inside the completion handler.

override func viewDidLoad() {
    super.viewDidLoad()

    // do whatever you want to let the user know that something asynchronous
    // is happening, e.g. add a spinning `UIActivityIndicatorView` or whatever

    userBinfo { user in
        self.userBinfos = user

        // Update the UI here, including removing anything we presented to let
        // the user know that the asynchronous process was underway. If you were
        // dealing with UITableView`, you'd call `tableView.reloadData()` here.
    }  

    // but not here
}

这篇关于使用完成块之外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆