【问题标题】:NSTimer is not fired in React Native componentNSTimer 在 React Native 组件中没有被触发
【发布时间】:2015-08-10 08:34:02
【问题描述】:

我有一个 React-Native 组件。我想安排一个 NSTime,但它从未被解雇,sendIt 从未被调用

- (void)sendEvent {  
  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setndIt:) userInfo:nil repeats:YES];
}

- (void)sendIt:(NSTimer *)timer {
  NSLog(@"Event fired");
}

【问题讨论】:

  • 我在swift中有一个similar issue,我可以通过在主线程上启动计时器来解决它。

标签: ios react-native


【解决方案1】:

解决方案: 这是由于 React Native 如何与 NSRunLoop 一起工作。
您需要将 NSTimer 添加到mainRunLoop

- (void)sendEvent {        
  NSTimer *timer = [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(sendIt:) userInfo:nil repeats:YES];
  [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)sendIt:(NSTimer *)timer {
  NSLog(@"Event fired");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多