(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

本文介绍了iOS开发中非主线程Runloop的运行机制,强调了手动开启Runloop的重要性,通过示例代码展示了如何在后台线程中使用NSTimer,并解释了如果不手动开启Runloop,定时器只会回调一次的原因。
1万+

被折叠的 条评论
为什么被折叠?



