我有一个带标签的 View
这些标签将在 viewdidload 时具有新值
在 viewdidload 中调用了 2 种方法。
第一个将调用另一个类的方法,该方法将有一个 NSURLSessionDataTask
,其中将有 completionHandler
block 。
我想将 session NSHTTPURLResponse
中这些标签的新值设置为字典对象。
问题是,当 View 加载时,标签的值为空。
我知道这是因为 View 在第一个方法完成之前调用了一个方法,这就是 block !
我不知道如何让第一个方法完成然后调用第二个。
我试过了
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1
[AnotherClass method:argu anotherArgu:argue2]; // calling the method from the otherclass, this method have completionHandler block
_curentObj = [AnotherClass currentObje];
dispatch_async(dispatch_get_main_queue(), ^{ // 2
[self setupUserLabel]; // 3 this method will set the new value for the label from _currentObj but it's null
});
});
我不知道如何让它完成第一个调用,即 [AnotherClass method:argu anotherArgu:argue2];
然后在完成并完成后调用 viewdidload 中的另一个。
请帮忙
谢谢
最佳答案
你需要调整一下你的想法。您的结果可能在 viewDidLoad 中不可用。相反,您启动 NSURLSessionDataTask,并在完成处理程序中获得所需的结果,根据需要格式化它们,然后在完成 block 内调用dispatch_async(dispatch_get_main_queue()) 来更改 UI。强>.
在后台线程上调用完成 block ,执行准备工作,然后使用dispatch_async 调用代码以将新数据安装到主线程上的 UI 中。
关于ios - GCD NSURLSession completionHandler block 返回值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35531606/