Objective-C Programming summary (Part 1) runloop

Source: Internet
Author: User

I am too lazy to write this Summary. I directly transferred it to an article on the Internet.Article: Http://blog.csdn.net/favormm/article/details/6928432

First look at the two runloop example, source: http://paste.lisp.org/display/86524 first: # includecorefoundation/corefoundation. H static void _ perform (void * info _ unused) {printf (Hello \ n);} static void _ timer (cfrunlooptimerreftimer _

Original article: http://blog.csdn.net/favormm/article/details/6928432

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,
  26. _ Timer, & timer_context );
  27. Cfrunloopaddtimer (cfrunloopgetcurrent (), timer, kcfrunloopcommonmodes );
  28. Cfrunlooprun ();
  29. Return 0;
  30. }

Second:

    1. # include
    2. # include
    3. int
    4. main ()
    5. {
    6. dispatch_source_t source, timer;
    7. source = dispatch_source_create (dispatch_source_type_data_add, 0, 0,
    8. dispatch_get_global_queue (dispatch_queue_priority_default, 0);
    9. dispatch_source_set_event_handler (source, ^ {
    10. printf ("Hello \ n");
    11. });
    12. dispatch_resume (source);
    13. timer = dispatch_source_create (dispatch_source_type_timer, 0, 0,
    14. dispatch_get_global_queue (dispatch_queue_priority_default, 0);
    15. dispatch_source_set_timer (timer, dispatch_time_now, 1ull * nsec_per_sec, 0);
    16. dispatch_source_set_event_handler (timer, ^ {
    17. dispatch_source_merge_data (source, 1);
    18. });
    19. dispatch_resume (timer);
    20. dispatch_main ();
    21. }

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. TheCodeThe nsport part does not work on iOS, but it can be implemented using its CF method, which is shown 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/

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

 

So far, OC LanguageThe summary is complete. It's time to start learning about the iPhone SDK. So far, although I have had some knowledge about the language, I still can't do anything. It belongs to the children before the kindergarten class :)

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.