runloop 原理

与其相关的概念

  • NSTimer
  • UIEvent
  • Autorelease
  • NSDelayedPerforming、NSThreadPerformAddition
  • CADisplayLink、CATransition、CAAnimation

Autorelease在什么时候释放?

UIKit通过RunloopObserver在Runloop两次Sleep之间对AutoreleasePool进行Pop和Push,将这次Loop中产生的Autorelease对象释放

runloop 和 线程有什么关系

崩溃的时候重启Runloop

- (void)restartRunloop{
    CFRunLoopRef runloop = CFRunLoopGetCurrent();
    NSArray *allModes = CFBridgingRelease(CFRunLoopCopyAllModes(runloop));
    while (1) {
        for (NSString *mode in allModes){
            CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);
        }
    }
}