Methods for delayed execution of iOS

Source: Internet
Author: User

1. The most straightforward method PerformSelector:withObject:afterDelay: The disadvantage of this approach: Write a method for the time delay 2. Use categories, execute with Bolck
 on     @implementationNSObject (Performblockafterdelay) Geneva      Geneva- (void) Performblock: (void(^) (void)) BlockGenevaAfterdelay: (nstimeinterval) Delay to     { .block =[[block copy] autorelease]; -[Self performselector: @selector (fireblockafterdelay:) ,Withobject:block theAfterdelay:delay];Ten     } One       A- (void) Fireblockafterdelay: (void(^) (void) ) Block { -block (); -     } the       -     @end

3. Using GCD
1     void 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 code has been killed in IOS6 now with Dispatch_get_global_queue (dispatch_queue_priority_ high,0) The first parameter is the priority, the second parameter is 0, or nil if you want to work with the interfacedispatch_get_main_queue(); Reference: http://www.cnblogs.com/guwandong/archive/2012/08/08/2626211.html
1 dispatch_after (Dispatch_time (Dispatch_time_now, nsec_per_sec*10), ^{2                          Dispatch_async (Dispatch_get_main_queue (), ^{3                             [self alertlabelwithstr:@ "  Scheduled synchronization successful "];//Execution Method 4                        }"; 5                     });

The second parameter of Dispatch_time is how many nanoseconds execute

4. May not be a good way to use the completion parameter of animation
1     [UIView animatewithduration:0.0 delay:5.0 options:uiviewanimationoptionallowuserinteraction animations:^{2     } completion:^(BOOL finished) {3         //  Do stuffhere4     }];

5. Using Nsoperationqueue, execute in the next main loop of the application:
1     
This and call Performselector:with Afterdelay of 0.0f equivalent

Methods for delayed execution of iOS

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.