How to Use RunLoop in Cocoa

Source: Internet
Author: User

CocoaMediumRunLoopThis article describes how to use the input sources of CFRunLoop object monitoring task tasks and schedule and control them when they are ready for processing.

The Input Source sample may include the user's input device, network link, scheduled or time delay event, and asynchronous callback. Three types of objects can be monitored by run loop: sources, timers, and observers.

When these objects need to be processed, in order to receive the callback, you must first put these objects into runloop through CFRunLoopAddSource, CFRunLoopAddTimer, or CFRunLoopAddObserver. To stop receiving the callback, you can also remove an object from the run loop using CFRunLoopRemoveSource.

Runloop has different running modes, each of which has its own object set, runloop monitoring, and runs in this mode at the same time. Core Foundation defines a default mode kcfrunloopdefamode mode to hold objects. These objects should be monitored when the application or thread is idle. When an object is added to an unknown mode, the additional mode is automatically created. Each runloop has its own independent mode set.

Core Foundation also defines a special pseudo-mode kCFRunLoopCommonModes to hold the objects that should be shared by the "common" Mode set. Call CFRunLoopAddCommonMode to add a mode set to the "common" mode. The default mode kCFRunLoopDefaultMode is always a member of the common mode set. The kCFRunLoopCommonModes constant will never be passed to CFRunLoopRunInMode. Each run loop has its own independent common mode set.

Each thread exactly has a runloop, which cannot be created or destroyed ., Core Foundation is created for you as needed. Through CFRunLoopGetMain, you can obtain the runloop of the current thread. Call lCFRunLoopRun to start the runloop of the current thread in the default mode until CFRunLoopStop is called to stop the runloop. You can also call CFRunLoopRunInMode to run the run loop of the current thread in the specified mode for a period of time or until the runloop is stopped. Runloop can only run when the request mode has at least one source or timer to monitor.

RunLoop can be run recursively. You can call CFRunLoopRun or CFRunLoopRunInMode internally in any run loop annotation. You can also create nested runloop and activate the stack in the current thread, there is no limit on the mode that can be run in the annotation. You can create another runloop to activate any feasible runloop mode, including any higher mode that has been run in the call stack.

Both Cocoa and Carbon are built on CFRunLoop to implement their own High-level Event loops. When writing a Cocoa or Carbon application, you can add your sources, timer, and observers to their run loop objects. Your object will be monitored as part of the regular application event loop. Use the nsunloop instance method getCFRunLoop to obtain the CFRunLoop corresponding to the cocoa run loop, and use the GetCFRunLoopFromEventLoop function in the carbon application.

Summary: DetailsCocoaMediumRunLoopI hope this article will help you learn how to use it!

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.