• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 何时使用 dispatch_get_main_queue

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:49:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 iOS 中学到了一条全局规则 -> 永远不要阻塞主线程。 但是,有几次我遇到了违反此规则的开源代码片段。

下面是两个这样的例子:

以下函数取自 https://github.com/piwik/piwik-sdk-ios/blob/master/PiwikTracker/PiwikTracker.m

- (void)startDispatchTimer {

 //在主线程运行循环
  __weak typeof(self)weakSelf = self;
  dispatch_async(dispatch_get_main_queue(), ^{

    [weakSelf stopDispatchTimer];

   //如果调度间隔为 0) {

     //在计时器上运行
      weakSelf.dispatchTimer = [NSTimer scheduleTimerWithTimeInterval:weakSelf.dispatchInterval
                                                                目标:弱 self 
                                                              选择器选择器(调度
                                                              用户信息:无
                                                               重复:否];

      NSLog(@"Dispatch timer 以间隔 %f 开始", weakSelf.dispatchInterval);

    }

  });

}

在上面的代码中,我一直试图理解为什么计时器对象需要主线程。像这样的事情与 UI 无关,仍然在主线程上完成。

另一个例子是著名的网络库 MKNetworkKit。 以下代码在 NSOperation 的 start 方法中。 https://github.com/MugunthKumar/MKNetworkKit/blob/master/MKNetworkKit/MKNetworkOperation.m

<上一页> dispatch_async(dispatch_get_main_queue(), ^{ self.connection = [[NSURLConnection alloc] initWithRequest:self.request 代表:自己 立即开始:否]; [self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [self.connection start]; });

所以我的问题是为什么人们使用主线程来执行不与 UI 相关的操作,它有什么好处。如果您不坚持它,它可能不会卡住您的应用程序,但为什么要冒险。



Best Answer-推荐答案


这两个例子都直接或间接地使用了 NSRunLoop 方法。在这些情况下,您应该从执行目标 NSRunLoop 的线程中调用方法。因此你需要 dispatch_get_main_queue()。

看看苹果关于NSRunLoop的文档https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsrunloop_class/reference/reference.html

Warning: The NSRunLoop class is generally not considered to be thread-safe and its methods should only be called within the context of the current thread. You should never try to call the methods of an NSRunLoop object running in a different thread, as doing so might cause unexpected results.

顺便说一句,NSRunLoop 似乎在 Core Foundation 中使用了 CFRunLoop,而 Core Foundation 是在 Apple 的开源许可下发布的。

http://opensource.apple.com/source/CF/CF-855.17/CFRunLoop.c

似乎 CFRunLoop 是线程安全的(我们可以看到很多 __CFRunLoopLock 和 __CFRunLoopUnlock 组合)。但无论如何你最好遵守文件

关于ios - 何时使用 dispatch_get_main_queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25795565/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap