【问题标题】:performSelector:withObject:afterDelay: -- can I be certain that this is run on the main thread?performSelector:withObject:afterDelay: -- 我可以确定这是在主线程上运行的吗?
【发布时间】:2014-12-18 13:33:19
【问题描述】:

我正在使用 iOS,我想在主线程上运行导入过程,同时显示进度界面。我认为我应该使用 performSelectorOnMainThread,但它不提供“withDelay”参数。如果我当前在主线程上,我可以确定以下代码将在主线程上执行吗?

- (void)tkContactsMultiPickerController:(TKContactsMultiPickerController *)picker didFinishPickingDataWithInfo:(NSArray *)contacts
{
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    [UIApplication sharedApplication].idleTimerDisabled = YES;

    [SVProgressHUD showWithStatus:[self importingString] maskType:SVProgressHUDMaskTypeBlack];

    [self performSelector:@selector(importContacts:) withObject:contacts afterDelay:.1];
}

【问题讨论】:

  • 您的措辞不清楚。如果tkContactsMultiPickerController: 在主线程上被调用,那么是的,importContacts: 也会在主线程上被调用。
  • 是的,tkContactsMultiPickerController 在主线程上被调用。我将尝试改写问题。
  • 实际上我最初确实有一些关于我是否在主线程上的问题,因为我使用 dispatch_async 访问地址簿,但我确实在整个代码中使用 [NSThread isMainThread] 进行日志验证我在主线程上并且使用上面的 performSelector 方法确实让我在主线程上。

标签: ios multithreading cocoa-touch thread-safety


【解决方案1】:

是的。 The documentation 明确表示:

在延迟后使用默认模式在当前线程上调用接收者的方法。

如果您需要从另一个返回主线程,您也可以使用dispatch_after() -- 主队列are guaranteed 上的任务要在主线程上运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多