这是最近在修改一个数据同步模块时发现的问题。整个数据同步的任务是在App启动后放在一个后台执行的线程中的,执行某个单条数据同步任务成功后,会使用html
[self performSelector:(nonnull SEL) withObject:(nullable id) afterDelay:(NSTimeInterval)]; spa
来执行下一个单条数据同步任务。经过调试,发如今执行到这行代码的时候,并无调用 SEL 的方法。在肯定存在这个方法后,一直没有想到缘由,因而就谷歌之。就是,performSelector withObject afterDelay 方法在子线程中,并不会调用SEL方法,而 performSelector withObject 方法会直接调用。缘由在于一下两点:线程
一、afterDelay方式是使用当前线程的定时器在必定时间后调用SEL,NO AfterDelay方式是直接调用SEL。
二、子线程中默认是没有定时器的。调试
因此,针对问的解决方法也有两种:orm
一、开启线程的定时器htm
[[NSRunLoop currentRunLoop] run]; get
二、使用dispatch_after来执行定时任务同步
- (void)functionToDelay:(SEL)function param:(nullable id)param afterDelay:(NSTimeInterval)delay {
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, delay*NSEC_PER_SEC);
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([self respondsToSelector:function]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:function withObject:param];
#pragma clang diagnostic pop
}
});
}
QQ技术交流群290551701 http://cxy.liuzhihengseo.com/556.html