在React Native组件中不会触发NSTimer

Kos*_*val 2 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");
}
Run Code Online (Sandbox Code Playgroud)

Kos*_*val 5

解决方案:这是由于React Native如何使用NSRunLoop.
您需要添加NSTimermainRunLoop

- (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");
}
Run Code Online (Sandbox Code Playgroud)