在我的应用程序中,我具有一个使NSRURLSession发出并使用发送NSURLRequest的功能
sesh.dataTaskWithRequest(req, completionHandler: {(data, response, error)
在此任务的完成块中,我需要进行一些计算,以将UIImage添加到调用的viewcontroller中。我有一个函子叫做
func displayQRCode(receiveAddr, withAmountInBTC:amountBTC)
进行UIImage添加计算。如果我尝试在完成块内运行添加视图的代码,则Xcode会引发错误,提示我在后台进程中无法使用布局引擎。所以我在SO上找到了一些代码,试图在主线程上将方法排队:
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(0.0 * Double(NSEC_PER_MSEC)))
dispatch_after(time, dispatch_get_main_queue(), {
let returned = UIApplication.sharedApplication().sendAction("displayQRCode:", to: self.delegate, from: self, forEvent: nil)
})
但是,我不知道如何在此函数调用中添加参数“ receiveAddr”和“ amountBTC”。我该怎么做,或者有人可以建议将方法调用添加到应用程序主队列的最佳方法?
dispatch_async(dispatch_get_main_queue(), {
let delegateObj = UIApplication.sharedApplication().delegate as YourAppDelegateClass
delegateObj.addUIImage("yourstring")
})
//Perform some task and update UI immediately.
DispatchQueue.global(qos: .userInitiated).async {
// Call your function here
DispatchQueue.main.async {
// Update UI
self.tableView.reloadData()
}
}
//To call or execute function after some time
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
//Here call your function
}
//If you want to do changes in UI use this
DispatchQueue.main.async(execute: {
//Update UI
self.tableView.reloadData()
})