iOS Development Runloop Learning: Three: Runloop related classes (source and observer)

Source: Internet
Author: User

One: Runloop related classes:

Where: Source0 refers to a non-port-based por, which is the handling of touch events, selector events, source1 refers to Port-based ports: some of the events that are handling the system

Note: After creating a runloop, there is a default mode mode, you can also specify the operating mode for Runloop, Runloop Boot must have a running mode, and in the operating mode must have a timer or one of the source events, Otherwise, Runloop will quit. Start Runloop must call the Start method

Two: Runloop Running processing logic

Runloop notifies the Observer that observer is about to enter Runloop, and notifies the Observer observer to handle the timer, notifies the Observer observer to handle SOURCE0 (not port-based port, that is, touch, scroll, Selector selector event), Runloop handles the Source0 event, and if there is no Source1 (System event) at this time after the Source0 event is processed, Runloop goes into hibernation until the external source0, Timer or external manual wake-up Runloop, if there is Source1 event, will handle the wake-up message received, and then continue to jump back to 2--processing timer--processing source0--in the check there is no Source1, Runloop is a dead loop, Finally, if the thread is hung up (that is, the thread will hang up after the task in the thread has been executed, even if you point to the thread with an external strong reference, the thread will still hang up, and if you want the thread to finish the task and not be hung up, you can manually turn on the Runloop, specify the mode of operation for Runloop, A source or timer must be specified for Runloop in mode, or Runloop will exit immediately, notifying observer of impending exit Runloop

2: Apple official edition

#import "ViewController.h"/** * 1: Add listener for Runloop: 1: First create listener 2: Add Listener*/@interfaceViewcontroller ()/** Notes*///@property (nonatomic, strong) pthread_t thread;@end@implementationViewcontroller#pragmaMark----------------------#pragmaMark Events-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//[self observer];//[Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (Task) Userinfo:nil Repeats:yes];[Nsthread detachnewthreadselector: @selector (Task) totarget:self Withobject:nil];}-(Ibaction) Sourcebtnclick: (ID) sender{NSLog (@"%s", __func__);}#pragmaMark----------------------#pragmamark-(void) task{NSLog (@"%s", __func__); //[Nsrunloop Currentrunloop] rununtildate:[];}-(void) observer{//1. Creating Listeners    /*first parameter: How to allocate storage space The second parameter: the state to listen to kcfrunloopallactivities all states the third parameter: the time to continuously listen to the fourth parameter: priority is always passed 0 the fifth parameter: when the state changes Time of the callback*/Cfrunloopobserverref Observer= Cfrunloopobservercreatewithhandler (Cfallocatorgetdefault (), Kcfrunloopallactivities, YES,0, ^(Cfrunloopobserverref Observer, cfrunloopactivity activity) {/*kcfrunloopentry = (1UL << 0), coming into runloop kcfrunloopbeforetimers = (1UL << 1), coming Timer Event Kcfrunloopbeforesources = (1UL << 2), the source event is about to be processed kcfrunloopbeforewaiting = (1UL << 5 ), coming into sleep kcfrunloopafterwaiting = (1UL << 6), awakened Kcfrunloopexit = (1UL << 7), Runloop Exit kcfrunloopallactivities = 0x0fffffffu*/        Switch(activity) { CaseKcfrunloopentry:nslog (@"coming into Runloop");  Break;  CaseKcfrunloopbeforetimers:nslog (@"The Timer event is about to be processed");  Break;  CaseKcfrunloopbeforesources:nslog (@"The source event is about to be processed");  Break;  CaseKcfrunloopbeforewaiting:nslog (@"coming into sleep");  Break;  CaseKcfrunloopafterwaiting:nslog (@"be awakened");  Break;  CaseKcfrunloopexit:nslog (@"Runloop Exit");  Break; default:                 Break;        }    }); /*first parameter: which runloop to listen to. The second parameter: the third parameter of the Observer: Run mode*/Cfrunloopaddobserver (Cfrunloopgetcurrent (), observer, Kcfrunloopdefaultmode); //Nsdefaultrunloopmode = = Kcfrunloopdefaultmode//Nsrunloopcommonmodes = = Kcfrunloopcommonmodes}@end

iOS Development Runloop Learning: Three: Runloop related classes (source and observer)

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.