NSTimer 不会在 React Native 组件中触发

NSTimer is not fired in React Native component

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

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