IOS running loop Summary

Source: Internet
Author: User

First look at two runloop examples, source: http://paste.lisp.org/display/86524

First:

 

  1. # Include <CoreFoundation/CoreFoundation. h>
  2. Static void
  3. _ Perform (void * info _ unused)
  4. {
  5. Printf ("hello \ n ");
  6. }
  7. Static void
  8. _ Timer (CFRunLoopTimerRef timer _ unused, void * info)
  9. {
  10. CFRunLoopSourceSignal (info );
  11. }
  12. Int
  13. Main ()
  14. {
  15. CFRunLoopSourceRef source;
  16. CFRunLoopSourceContext source_context;
  17. CFRunLoopTimerRef timer;
  18. CFRunLoopTimerContext timer_context;
  19. Bzero (& source_context, sizeof (source_context ));
  20. Source_context.perform = _ perform;
  21. Source = CFRunLoopSourceCreate (NULL, 0, & source_context );
  22. CFRunLoopAddSource (CFRunLoopGetCurrent (), source, kCFRunLoopCommonModes );
  23. Bzero (& timer_context, sizeof (timer_context ));
  24. Timer_context.info = source;
  25. Timer = CFRunLoopTimerCreate (NULL, CFAbsoluteTimeGetCurrent (), 1, 0, 0, _ timer, & timer_context );
  26. CFRunLoopAddTimer (CFRunLoopGetCurrent (), timer, kCFRunLoopCommonModes );
  27. CFRunLoopRun ();
  28. Return 0;
  29. }

 

Second:

 

  1. # Include <dispatch/dispatch. h>
  2. # Include <stdio. h>
  3. Int
  4. Main ()
  5. {
  6. Dispatch_source_t source, timer;
  7. Source = dispatch_source_create (DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ));
  8. Dispatch_source_set_event_handler (source, ^ {
  9. Printf ("hello \ n ");
  10. });
  11. Dispatch_resume (source );
  12. Timer = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ));
  13. Dispatch_source_set_timer (timer, DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC, 0 );
  14. Dispatch_source_set_event_handler (timer, ^ {
  15. Dispatch_source_merge_data (source, 1 );
  16. });
  17. Dispatch_resume (timer );
  18. Dispatch_main ();
  19. }

 

The function is to add two input sources to the main thread. One is timer and the other is custom input source. Then, the timer calls the callback method by touching the source from the source. Here, timer triggers the source to call the callback method. However, this is useful in multi-threaded development. We can add this custom source to the runloop of the sub-thread, and then trigger the source in the main thread, in this way, the callback method can be called in the Child thread. What has it been like for a long time? Saves electricity because runloop is usually sleep and starts to work only when an event is triggered. This is a bit similar to waitforsingleobject in windows, and similar to semaphores and events in multiple threads.

 

What is the input source? The Input Source sample may include a user input device (such as clicking a button), a network link (socket receives data), a scheduled or time-delay event (nst ), there is also an asynchronous callback (NSURLConnection asynchronous request ). Then we classified them into three types that can be monitored by runloop: sources, timers, and observers.

Runloop is described in detail in the Apple documentation. The Chinese version is available for reference below. The code in the document about NSPort does not work on iOS, but it can be implemented using its CF method and displayed in my demo.

 

Each thread has its own runloop. The main thread is enabled by default. The created sub-thread must be enabled manually because NSApplication only starts main applicaiton thread.

Runloop without source will automatically end.

The event is handled by the nsunloop class.

RunLoop monitors the Input Source of the operating system. If there is no event data, it does not consume any CPU resources.

If event data exists, run loop sends a message to notify each object.

Use currentRunLoop to obtain the reference of runloop

Send a run message to runloop to start it.

 

 

This document describes the following four scenarios:

1. Use a port or custom Input Source to communicate with other threads

2. The timer is used in the child thread.

3. Use any javasmselector in cocoa to run the method in the thread.

4. Enable the thread to perform periodic tasks (I understand this as 2)

If we use NSURLConnection asynchronous requests in the subthread, we also need to use runloop. Otherwise, the corresponding delegate method cannot be triggered when the thread exits.

For solutions, see:

Http://www.cocoabyss.com/foundation/nsurlconnection-synchronous-asynchronous/

Http://www.wim.me/nsurlconnection-in-its-own-thread/

 

See the demo for details.

 

 

Refer:

Http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

Http://www.wim.me/nsurlconnection-in-its-own-thread/

Http://xubenyang.me/384

Http://iphonedevelopmentbits.com/event-driven-multitasking-runloopssymbian-ios

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.