IOS RunLoop, iosrunloop

Source: Internet
Author: User

IOS RunLoop, iosrunloop

  • What is RunLoop?
    • Running cycle
    • A thread corresponds to a RunLoop. The main thread's RunLoop is started by default, and the Child thread's RunLoop must be started manually (call the run method)
    • RunLoop can be started in only one Mode. If no Source (Sources0, Sources1) or Timer exists in the current Mode, exit RunLoop directly.
  • RunLoop Function

    • Keep programs running continuously
    • Process various events in the App (such as touch events, Timer events, Selector events)
    • CPU resources are saved and program performance is improved: When you do things, you need to rest ......
  • Simulate the internal implementation of RunLoop

    • In fact, it is a do-while loop in which various tasks (such as Source, Timer, and Observer) are continuously processed)
    • Void message (int num) {printf ("execute the % I task", num);} int main (int argc, const char * argv []) {do {printf ("is there a problem? I slept "); int number; scanf (" % I ", & number); message (number);} while (1); return 0 ;}

 

  • Get RunLoop object

    • RunLoop object
      • Nsunloop
      • CFRunLoopRef
    • Foundation
      [Nsunloop currentRunLoop]; // obtain the RunLoop object of the current thread [nsunloop mainRunLoop]; // obtain the RunLoop object of the main thread
    • Core Foundation
      CFRunLoopGetCurrent (); // obtain the RunLoop object CFRunLoopGetMain () of the current thread; // obtain the RunLoop object of the main thread
  • RunLoop Structure

  • The RunLoop object corresponding to CFRunLoopRef
    • CFRunLoopModeRef indicates the running Mode of RunLoop. By default, five modes are registered.
      • NSDefaultRunLoopMode: Default Mode of the App. Generally, the main thread runs in this Mode.
      • UITrackingRunLoopMode: interface tracking Mode, used for ScrollView tracking touch sliding, ensure that the interface sliding is not affected by other modes
      • Nsunloopcommonmodes: This is a placeholder Mode, not a real Mode
    • CFRunLoopTimerRef is a time-based trigger.
      • CFRunLoopTimerRef basically refers to NSTimer, which is affected by the RunLoop Mode.
    • CFRunLoopSourceRef is the event source (input source)
    • CFRunLoopObserverRef is an observer and can monitor the status change of RunLoop.
    • // 1. create Observer // The first parameter: memory used to allocate the observer object // The second parameter: used to set the event to be followed by the observer // The third parameter: this parameter is used to identify whether the observer is executed when it first enters the run loop or when it enters the run loop processing. // The fourth parameter is used to set the priority of the observer. // The Fifth parameter: the callback block CFRunLoopObserverRef observer = callback (CFAllocatorGetDefault (), callback, YES, 0, ^ (callback observer, CFRunLoopActivity activity) {switch (activity) {case kCFRunLoopEntry: NSLog (@ "Coming Soon loop"); break; case kCFRunLoopBeforeTimers: NSLog (@ "Coming Soon timers"); break; case kCFRunLoopBeforeSources: NSLog (@ "Coming Soon sources "); break; case kCFRunLoopBeforeWaiting: NSLog (@ "coming to sleep"); break; case kCFRunLoopAfterWaiting: NSLog (@ "waking up from sleep"); break; case kCFRunLoopExit: NSLog (@ "about to exit loop"); break; default: break ;}}); // 2. add listener/* First parameter: The RunLoop to which the listener is added. Second parameter: The Observer object to be added. Third parameter: In which mode the listener */CFRunLoopAddObserver (CFRunLoopGetMain (), observer, kCFRunLoopDefaultMode); // 3. Release observer CFRelease (observer );

       

  • RunLoopRunLoop processing logic
  • RunLoopRunLoop Application
    • NSTimer
      • It can only be run under the specified model
      • NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    • ImageView display
      • You can only set images under the specified model.
    • PerformSelector
      • Can only be called under the specified model
      • [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"lnj"] waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];

         

    • Resident thread
      • You must call run to execute an endless loop.
      • Source/timer must be included in the model of nsunloop, so that the endless loop will not exit.
      • NSRunLoop *runloop = [NSRunLoop currentRunLoop];[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];[runloop run]

         

    • Auto Release pool
      • Activities = 0x1 = 11: about to enter RunLoop: create an automatic release pool activities = 0xa0 = 160 = 128 + 3232: about to sleep: Release the previous automatic release pool, create a new auto release pool 128: About to exit RunLoop: Release Auto Release pool

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.