在React Native组件中不会触发NSTimer

时间:2015-08-10 08:34:03

标签: ios react-native

我有一个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");
}

1 个答案:

答案 0 :(得分:5)

<强>解决方案: 这是由于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");
}