IPhone application development-Overview of the nsunloop

Source: Internet
Author: User

IPhoneAboutNsunloopIs the content of this article,NsunloopIt is a more advanced message processing mode, and he is better at abstracting and encapsulating the message processing process, in this way, you do not need to process some very trivial and low-level specific messages.NsunloopEach message is packaged in input source or timer source.

1. What is nsunloop?

We often see this Code:

 
 
  1.  - (IBAction)start:(id)sender  
  2. {  
  3. pageStillLoading = YES;  
  4. [NSThread detachNewThreadSelector:@selector(loadPageInBackground:)toTarget:self withObject:nil];  
  5. [progress setHidden:NO];  
  6. while (pageStillLoading) {  
  7. [NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  8. }  
  9. [progress setHidden:YES];  
  10. }  

This piece of code is amazing because it will "Pause" the code and the program running will not be affected by a while loop. After [progress setHidden: NO] is executed, the entire function is paused and stops in the loop. After the operations in loadPageInBackground are completed, the [progress setHidden: YES] can be run. This is a brief introduction and the logic is clear. If you do not do this, you need to call [progress setHidden: YES] In the loadPageInBackground to indicate that the load is complete. It seems that the Code is not compact and error-prone.

So what is nsunloop? In fact, the essence of the nsunloop is a message mechanism processing mode. If you have some knowledge of vc ++ programming, there are a series of important functions in windows, such as SendMessage, PostMessage, and GetMessage, which are APIs for message passing and processing.

But when you enterCocoaIn the programming world, I don't know if you are too fast and too hasty to ignore this very important issue. Cocoa does not mention any APIs for message processing, developers have never cared about the message transmission process on their own, as if everything was so natural and as natural as nature? In Cocoa, you no longer need to define a macro such as WM_COMMAD_XXX to identify a message, or perform special processing on a specific message in switch-case. Is there no message mechanism in Cocoa? The answer is no, but Apple adopts a better model when designing message processing, that is, RunLoop.

2. Working Principle of nsunloop

Next, let's take a look at the specific working principle of the nsunloop. The first is the statement provided in the official documentation, as shown in the figure:

All "messages" are added to the nsunloop, here, these messages are divided into "input source" and "Timer source", and check whether there is an event in the loop. If necessary, call the corresponding function for processing. For a clearer explanation, we will compare the message processing processes of VC ++ and iOS.

In VC ++, After all initialization is complete, the program starts such a loop. The code is intercepted from the slides of the sir mfc Program Design Course ):

 
 
  1. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR  lpCmdLine,int nCmdShow){  
  2. ...  
  3. while (GetMessage(&msg, NULL, 0, 0)){  
  4. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){  
  5. TranslateMessage(&msg);  
  6. DispatchMessage(&msg);  
  7. }  
  8. }  
  9. }  

We can see that after GetMessage, we can distribute and process the message. In iOS, the main function only calls UIApplicationMain, so we can think that UIApplicationMain will enter this situation after Initialization is complete:

 
 
  1. int UIApplicationMain(...){  
  2. ...  
  3. while(running){  
  4. [NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  5. }  
  6. ...  
  7. }  

Therefore, in UIApplicationMain, the program is also constantly processing runloop. I just mentioned that the nsunloop is a better message processing mode, and he is better at abstracting and encapsulating the message processing process, in this way, you do not need to process some very trivial and low-level specific messages. In the nsunloop, each message is packaged in the input source or timer source, when processing is required, the corresponding object processing function is called directly.

So what you feel for external developers is to add source/timer to the runloop, and then something like [handler action] will happen as appropriate. Most of the time, you don't even feel the first half of the process. You just feel a function call to an object.

For example, when a UIView is touched, a function such as touchesBegan/touchesMoved is called. You may think, "Damn it, I don't know where I was notified of a touch message, these processing functions are called !?" Therefore, there are some messages, but runloop has already helped you! To prove my point of view, I intercepted a call stack of debug touchesBegan,

Now let's look at the example of "Pausing" the Code just now. Do you have a deeper understanding of it?

Summary:IPhoneApplication DevelopmentNsunloopThe overview is complete. I hope this article will help you!

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.