IOS RunLoop 3. iosrunloop
After two introductions, I want to have a simple understanding of RunLoop, at least I don't know anything about it.
In this article, I would like to make a simple supplement to "CFRunLoopObserverRef.
Let's talk about it before adding it.
ARC is rarely seen in current development.
But that is for the OC object.
CFRunLoopObserverRef belongs to CF (Core Foundation)
So we need to manually release it.
The rules are as follows:
All objects created by functions with creat copy retain and other components must be released at the end, that is, Release.
/// ViewController. m // CX RunLoop analysis // Created by ma c on 16/3/29. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// Add the status kCFRunLoopEntry = (1UL <0), kCFRunLoopBeforeTimers = (1UL <1 ), kCFRunLoopBeforeSources = (1UL <2), kCFRunLoopBeforeWaiting = (1UL <5), priority = (1UL <6), kCFRunLoopExit = (1UL <7), priority = 0x0FFFFFFFU */CFRunLoopObserverRef observe = timeout (CFAllocatorGetDefault (), kCFRunLoopAllActivities, YES, 0, ^ (CFRunLoopObserverRef observer, CFRunLoopActivity) {NSLog (@ "Listening to runloop -- % zd", activity );}); // Add the observer CFRunLoopAddObserver (CFRunLoopGetCurrent (), Observe, kCFRunLoopDefaultMode); // CF Core Foundation/* all objects created by functions with creat copy retain and other components must be released at the end, that is, Release. */CFRelease (observe);} @ end
The above is a simple supplement to CFRunLoopObserverRef.
Here we will briefly introduce the processing logic of RunLoop.
Runloop processing logic:
1. Notify the Observer to enter the loop.
2. Notify Observer to process timer
3. Notify Observer to process Source0
4. Process Source0
5. If Source1 exists, skip to step 1.
6. Notify the Obesrcer that the thread is about to sleep.
7. Sleep, waiting to wake up
8. Notify the Observer that the thread has just been awakened.
9. process the message received during wake-up and then jump back to 2.
10. Notify Oberver that the Loop is about to exit.