gpt4 book ai didi

iphone - 如何在后台运行代码后在主线程上更新数据?

转载 作者:可可西里 更新时间:2023-11-01 05:55:22 25 4
gpt4 key购买 nike

我有这个 dispatch_queue 代码,我用它来发出 3 个不同的数据请求。然后我在主线程上更新 tableView。我还能在主线程中放入什么?我正在使用此 [self requestExtendedData]; 从 Web 服务下载数据,然后对其进行解析并设置为 UILabels 等...

我的问题是:如果我有 [self requestExtendedData]; 在后台线程中运行,我如何在主线程中使用此数据的内容更新 UILabel?我应该把其他所有东西都放在主线程区域吗?所有的 UIView、UILabels 和 UIButton 对象等...

感谢帮助

dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);

// execute a task on that queue asynchronously
dispatch_async(jsonParsingQueue, ^{

[self requestUserData]; // request user comments data
[self requestExtendedData]; // request extended listing info data
[self requestPhotoData]; // request photo data


// once this is done, if you need to you can call
// some code on a main thread (delegates, notifications, UI updates...)
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];


});
});

// release the dispatch queue
dispatch_release(jsonParsingQueue);

最佳答案

苹果文档是这么说的:

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

这意味着您应该将所有的 UIViewUILabelsUIButton 对象代码放在主线程中,如下所示:

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
// Your UI update code here
});

关于iphone - 如何在后台运行代码后在主线程上更新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18156369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com