gpt4 book ai didi

objective-c - iOS dispatch_async,在 iOS 4 上崩溃

转载 作者:行者123 更新时间:2023-11-28 22:53:35 24 4
gpt4 key购买 nike

我在不同线程上向服务器发出服务器请求。当他们完成加载时,我得到一个更新 tableView 的委托(delegate)回调。起初,我注意到一些奇怪的行为,并认为这可能是因为某些 UI 元素没有在主线程上执行。

我添加了下面的代码,解决了这个问题,但是代码在 iOS 4 设备上崩溃了(事实证明 DISPATCH_QUEUE_PRIORITY_BACKGROUND 与 iOS 4 不兼容)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
//Update view code etc...
});
});

如果我删除第一个 dispatch_async,只调用 dispatch_get_main_queue,代码运行正常:

dispatch_async(dispatch_get_main_queue(), ^(void) {
//Update view code etc...
});

在将我的更新发送给 Apple 之前,我想问一下这是否可以?代码能否在 iOS 4 设备上正确运行?

提前致谢。

最佳答案

DISPATCH_QUEUE_PRIORITY_BACKGROUND 仅适用于 iOS 5 及更高版本

来自苹果文档

DISPATCH_QUEUE_PRIORITY_BACKGROUND Items dispatched to the queue run at background priority; the queue is scheduled for execution after all high priority queues have been scheduled and the system runs items on a thread whose priority is set for background status. Such a thread has the lowest priority and any disk I/O is throttled to minimize the impact on the system. Available in iOS 5.0 and later.

因此,如果您也在考虑为 iOS 4 分发您的应用程序,那么您不能使用它

来源apple docs

但是,如果您删除 DISPATCH_QUEUE_PRIORITY_BACKGROUND,您当前的代码将仅在主 ui 线程中运行,这可能会导致您在执行远程请求时速度变慢

关于objective-c - iOS dispatch_async,在 iOS 4 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284656/

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