【问题标题】:Is this correct for a NSTimer to get run on the main thread?这对于 NSTimer 在主线程上运行是否正确?
【发布时间】:2019-03-21 23:51:57
【问题描述】:

你好我想在主线程上运行一个 NSTimer 我不确定它们是默认在主线程上运行还是我必须做一个特殊的实现?感谢任何可以提供帮助的人

@interface RootViewController : UIViewController {

        NSTimer *minutePassed;
    }

    - (void)adViewDidLoad
    {

    minutePassed = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(callMinutedPassed) userInfo:nil repeats:NO];

    }

    -(void)callMinutePassed
    {
    NSLog("Minute Passed");
    }

【问题讨论】:

标签: ios objective-c nstimer


【解决方案1】:

scheduledTimer(timeInterval:target:selector:userInfo:repeats) 状态 Creates a timer and schedules it on the current run loop in the default mode. 的文档。这意味着在您的实例中,它在主线程上运行。我假设当您说 -(void)adViewDidLoad 时,您的意思是 -(void)viewDidLoad()

【讨论】:

  • 如果我有这样的事情并且应用程序进入后台,倒计时会一直计数到 60 秒还是会暂停?, minutePassed = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector( callMinutePassed) userInfo:nil repeats:NO];
猜你喜欢
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
相关资源
最近更新 更多