-(Bool) runmode :( nsstring *) mode beforedate :( nsdate *) Detailed description of the limitdate Method

Source: Internet
Author: User
The first is part of the concept of run loop, which is used to loop and process events. Specifically, there are two aspects: 1. scheduled task startup (generally used in collaboration with timer); 2. event processing. 

In a single-threaded app, you do not need to pay attention to the run loop, but it does not mean no. When the program starts, the system has added the run loop to the main thread. It ensures that our main thread is in a "Waiting" state after it is run (rather than running it once like some command line programs ), at this time, if an event is received (timer's scheduled time is reached or messages from other threads), the task will be executed; otherwise, the task will be in sleep state.

If we want to write multi-threaded programs, we may need to manage the run loop by ourselves.
The following describes the parameters in the method proposed by the author:
Runmode: nsdefaultrunloopmode, which can be understood as a "filter". We can only monitor the events we care about. Generally, nsdefaultrunloopmode is the most commonly used.

The run loop startup method is written by LZ, which is described as follows:
Runs the loop once, blocking for input in the specified mode until a given date.
Start the run loop once and wait for the input in a specific run loop mode.

If no input source, timer, or limitdate is appended, run loop will exit and no is returned.

-(void)getServer:(NSDictionary *)userInfo//-(void)getIMMessageServer:(NSString *)ip Port:(NSString *)port{NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@/Services/AccessGate/?tag/im", ip,port]];NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];while (!finished) {[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];}[pool release];}


We can always request a file until we get the file, that is, finished = Yes



Run loop is used in the following scenarios:
1. Use port or custom Input Source to communicate with other threads
2. Use timer in the thread (non-main thread)
3. Use the performselector... series (for example, performselecw.thread ,...)
4. Use threads to execute periodic work

You can also refer to the following example.

While (done)
{
[Nsunloop currentrunloop] runmode: currentmode beforedate: [nsdate datewithtimeintervalsincenow: 10];
}

In the above section, it seems that the program has entered an endless loop, but this is not the case.
This program means:
If the current thread has an event in the currently set runmode, The runloop starts to process the corresponding event. If no event occurs, runloop starts the runloop of the current thread every 10 seconds.
If the runloop is successfully started at a time [nsunloop currentrunloop] runmode: currentmode beforedate: [nsdate datewithtimeintervalsincenow: 10]; The returned value is yes, the success of the startup involves two scenarios: time-based triggering and 10-second-based triggering. If the startup fails, false is returned.
At this point, I may not understand why we need to create a loop and why we need to use runloop. I didn't quite understand it at the beginning.
Here is an explanation:
When we touch the screen on the iOS device, the corresponding tounch event will be called. Why? In fact, this place has credit for runloop.
In fact, runloop is a time processing mechanism, similar to the director of a workshop (I don't know if this metaphor is appropriate ), the Director is responsible for handling specific types of events on the workshop assembly line (here the specific event is runmode) this event can include security events, mechanical time and so on. The traditional method of the director's processing time can be to patrol the production line once every minute (corresponding to the CPU idling round-robin Message Queue method). This method is more labor-consuming (CPU resources, power ), when a problem occurs, he finds the corresponding worker to handle the problem. Here, the worker corresponds to the time processing function. Another way is that the director is usually sleeping and playing mahjong, when a problem occurs in the production line, if it is his responsibility, the system will directly send a text message to notify him, and then notify the corresponding worker to handle the problem after receiving the message, the ten seconds above (this can be changed) indicates that if no message is sent to the workshop within ten seconds, the Director will patrol the workshop once. However, the director did not receive the event processing message during these 10 seconds, so he usually leaves when he arrives at the workshop (the corresponding runloop starts and ends without event processing ).
Therefore, the benefits of using runloop are obvious.



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.