[Ios] ios delayed execution method and ios delayed execution Method

Source: Internet
Author: User

[Ios] ios delayed execution method and ios delayed execution Method
1. the most direct method is javasmselector: withObject: afterDelay: the disadvantage of this method: Write a method for the delay each time. use class and use BOLCK to execute [Code] c #/cpp/oc code:

01 @implementation NSObject (PerformBlockAfterDelay)
02  
03 - (void)performBlock:(void (^)(void))block
04           afterDelay:(NSTimeInterval)delay
05 {
06     block = [[block copy] autorelease];
07     [self performSelector:@selector(fireBlockAfterDelay:)
08                withObject:block
09                afterDelay:delay];
10 }
11  
12 - (void)fireBlockAfterDelay:(void (^)(void))block {
13     block();
14 }
15  
16 @end
3. Use GCD [Code] c #/cpp/oc code:
1 void RunBlockAfterDelay(NSTimeInterval delay, void (^block)(void))
2 {
3     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*delay),
4       dispatch_get_current_queue(), block);
5 }
Poolo: note that the dispatch_getcurrent_queue () method in the figure has been kill in ios6. Now the first parameter of dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0) is priority, the second parameter is 0 or nil if you want to operate on the interface, use dispatch_get_main_queue (); reference: http://www.cnblogs.com/guwandong/archive/2012/08/08/2626211.html 4. this may not be a good method. Use the [Code] c #/cpp/oc code of the completion parameter of animation:
1 [UIView animateWithDuration:0.0 delay:5.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
2 } completion:^(BOOL finished) {
3     //do stuff here
4 }];
5. Use NSOperationQueue to run in the next main loop of the application: [Code] c #/cpp/oc code:
1 [[NSOperationQueue mainQueue] addOperationWithBlock:aBlock];
This is equivalent to calling mongomselector: with afterDelay of 0.0f.
In iOS, how does one implement a function to set the time to complete, for example, execute-(void) stop; it takes 5 seconds, so it may not be executed

[Self defined mselector: @ selector (function name) withObject: nil afterDelay: 5.0f]
Condition: in the main thread of uiviewController
[NSTimer scheduledTimerWithTimeInterval: 5.0f target: self selector: @ selector (function name) userInfo: nil repeats: NO]; in a specific function, run the code, call NSTimer invalidate to destroy timer after execution.
[NSThread sleepForTimeInterval: 5.0f]; [method for delayed execution];
Condition: the master thread or sub-thread can


The didSelectRowAtIndexPath method of UITableView is developed for IOS.

What are the operations on clicking cell? Is the operation complicated? If there is no operation, it should respond immediately. What does the static table mean? It is also related to poor machines. If you do not have much data, you should respond immediately. If you click cell to make a response to bring up a page, and the page Initialization is very complex, it will not pop up immediately.

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.