iOS RunLoop Summary

RunLoop Profile

RunLoop actually a cycle, "receiving messages -> Wait -> handle" this cycle, to manage an event or message, such as a touch event, UI refresh event, timer events. Instantly wake handle the event when the message came, in the absence of handling events sleep, avoid resource consumption.

iOS, two such objects: NSRunLoop and CFRunLoopRef.
CFRunLoopRef CoreFoundation within the frame. NSRunLoop CFRunLoopRef package is based, it provides an object-oriented API.

CFRunLoopRef relevant code: http://opensource.apple.com/source/CF/CF-855.17/CFRunLoop.c

RunLoop thread

One correspondence between threads and runloop
CFRunLoop is based on pthread managed. Apple does not allow direct creation RunLoop, it only provides two functions automatically obtained: CFRunLoopGetMain () and CFRunLoopGetCurrent ().

288548-4582380b14eb40e0.png


RunLoop achieve

In CoreFoundation inside on RunLoop has five categories:

CFRunLoopRef
CFRunLoopModeRef
CFRunLoopSourceRef
CFRunLoopTimerRef
CFRunLoopObserverRef

Wherein CFRunLoopModeRef class does not expose outside, but is encapsulated by CFRunLoopRef interface.

A RunLoop contains several Mode, Mode and each contains a number of Source / Timer / Observer. Each time RunLoop main function calls can only specify a Mode, the Mode is referred CurrentMode. If you need to switch Mode, only to exit the Loop, and re-assign a Mode to enter. Doing so primarily to different groups of separating Source / Timer / Observer, let it affect each other.

1, CFRunLoopSourceRef is where the event generated

  • Source0 contains only a callback (function pointer), it does not take the initiative to trigger events.
  • Source1 and contains a mach_port a callback (function pointers), are used to send messages to each other through the cores and threads.

2, CFRunLoopTimerRef is a time-based trigger.
When added to RunLoop, registered corresponding RunLoop would point in time, when the time point to, RunLoop will be awakened to perform the callback

3, CFRunLoopObserverRef observer.
Each contains a callback Observer (function pointer), RunLoop when the state changes, the observer can receive a callback by this change.

Source / Timer / Observer is referred to as mode item, an item may be added simultaneously a plurality mode. But there will be no effect when a duplicate item is added to the same mode. If an item in a mode are not, RunLoop will exit without entering circulation.


288548-370e078802c6789b.png

RunLoop this object, in iOS implemented by CFRunLoop. Briefly, runloop source is used to monitor the input, scheduling process. Here input source may be an input device, a network, or a periodic time delay, asynchronous callback. RunLoop receives two types of input sources: one is the asynchronous messages from different applications or from another thread; another is synchronous or events from the reservation time interval is repeated.
288548-07df1a067abaa466.png

Application scenarios: RunLoop main thread, there are two preset Mode: kCFRunLoopDefaultMode and UITrackingRunLoopMode. Mode Both have been marked as "Common" attribute. App DefaultMode is a state in which the usual, TrackingRunLoopMode tracking state is ScrollView slide. When you create a Timer and added to the DefaultMode, Timer callback will be repeated, but this time a sliding TableView, RunLoop mode will be switched to TrackingRunLoopMode, Timer will not be callback time, and will not affect the operation of the slide .

Sometimes you need a Timer, in the two Mode can get a callback, one way is to join these two were the Timer Mode. Another way is added to the top of the Timer RunLoop of "commonModeItems" in. "CommonModeItems" RunLoop is automatically updated with all Mode "Common" properties go.

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        //do something
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

to sum up:
288548-a40bed6f09b3efe3.png

Reference

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
https://blog.ibireme.com/2015/05/18/runloop/
https://www.jianshu.com/p/23e3ff9619c3
https://www.cnblogs.com/kenshincui/p/6823841.html
http://mrpeak.cn/blog/ios-runloop/

Reproduced in: https: //www.jianshu.com/p/836b6b00d567

Guess you like

Origin blog.csdn.net/weixin_34124939/article/details/91141345