加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中如何使用GCD主线程上的参数调用方法?

发布时间:2020-12-14 06:13:28 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我有一个函数,使一个NSRURLSession和发出一个NSURLRequest使用 sesh.dataTaskWithRequest(req,completionHandler: {(data,response,error) 在这个任务的完成块,我需要做一些计算,添加一个UIImage到调用viewcontroller。我有一个func调
在我的应用程序中,我有一个函数,使一个NSRURLSession和发出一个NSURLRequest使用
sesh.dataTaskWithRequest(req,completionHandler: {(data,response,error)

在这个任务的完成块,我需要做一些计算,添加一个UIImage到调用viewcontroller。我有一个func调用

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_after
dispatch_async(dispatch_get_main_queue(),{

    let delegateObj = UIApplication.sharedApplication().delegate as YourAppDelegateClass
    delegateObj.addUIImage("yourstring")

 })

Swift 3:

DispatchQueue.main.async { 
        let delegateObj = UIApplication.sharedApplication().delegate as YourAppDelegateClass
        delegateObj.addUIImage("yourstring")
}

也用于在主队列之后分派

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    // your code here
}

用您的委托类替换YourAppDelegateClass

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读