(1)非主线程的runloop默认是关闭装填,需要手动开启
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelectorInBackground:@selector(fun) withObject:nil];
}
- (void)fun {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"回调");
}];
[[NSRunLoop currentRunLoop] run];//如果不手动开启runloop 定时器只会回调一次(fire 会立即执行一次回调)
[timer fire];
}
@end