AFNetworking的网络线程实现
+ (void)networkRequestThreadEntryPoint:(id)__unused object{ //放进自动释放池 @autoreleasepool{ //获取当前的线程 [[NSThread currentThread] setName:@"AFNetworking"]; //设置NSRunLoop循环检测,检测inputSource,time source同步事件 //runloopmode是一个集合:监听事件源,定时器和需通知的runLoop observer //NSMachPort 是监听的事件源 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; [runLoop run]; } } //设定该线程为单例,只初始化一次 + (NSThread*)networkRequestThread{ static NSThread *_networkRequestThread = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate,^{ _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; [_networkRequestThread start]; }); return _networkRequestThread; }
tableView延迟加载图片
tableView的cell中如果有ImageView,在滚动时更新图片可能造成卡顿现象,可以在tableView滚动停止后设置图片内容:
UIImage *image = ...; [self.headerImageView performSelector:@selector(setImage:) withObject:image afterDelay:0 inModes:@[NSDefaultRunLoopMode]];
App崩溃后立即重启
//保存当前RunLoop 中的所有modes,,然后进行恢复 CFRunLoopRef runLoop = CFRunLoopGetCurrent(); NSArray *allModes = CFBridgingRelease(CFRunLoopCopyAllModes(runLoop)); while(true){ for(NSString *mode in allModes){ CFRunLoopRunInMode((CFStringRef)mode,1/MAXFLOAT,false); } }