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