Objective-c关于延迟调用

1.使用sleep(5)

会阻塞当前线程,不建议使用,若在主线程期调用,会使界面卡死,不能动

 

 

2、使用performSelector

 

[objc]  view plain copy
 
  1. - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;  

 

这个方法是单线程的,也就是说只有当前调用次方法的函数执行完毕后,selector方法才会被调用。

1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法

3、[UIView animateWithDuration

但不确定后台是否有不执行的问题

 

[objc] view plain copy
 
  1. [UIView animateWithDuration:0.0delay:5.0options:UIViewAnimationOptionAllowUserInteraction animations:^{  
  2.    
  3. } completion:^(BOOL finished) {  
  4.    
  5.     //do stuff here  
  6.    
  7. }];  

 

4.使用NSOperationQueue,

在应用程序的下一个主循环执行:

[代码]c#/cpp/oc代码:

[objc]  view plain copy
 
  1. [[NSOperationQueue mainQueue] addOperationWithBlock:aBlock];  



这个和调用performSelector: with afterDelay of 0.0f等价

 

5使用dispatch_after

 

[objc]  view plain copy
 
  1.       // after 0.01 sec, since in certain cases the sliding view is reset.  
  2.       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){  
  3.           NSLog(@"10s以后---");  
  4.       });  
  5.         
  6.         
  7. double delayInSeconds = 0.5;  
  8. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);  
  9. dispatch_after(popTime, _cacheInfoQueue, ^(void){  
  10.     NSLog(@"0.5s 以后-----");  
  11. });  



 

补充:点击打开链接

比如:

 

- (void)changeText:(NSString *)string

{

    label.text = string;

    NSLog(@"changeText:(NSString *)string");

}

 

- (void)changePopoverSize

{   

    [self performSelector:@selector(changeText:) withObject:@"Happy aha" afterDelay:1];

 

    NSLog(@"changePopoverSize#####end");

    sleep(5);

    NSLog(@"changePopoverSize-----end");

}

 

 

 

 

执行结果(注意时间):

 

2012-08-17 17:14:06.697 awrbv[1973:f803] changePopoverSize#####end

2012-08-17 17:14:11.698 awrbv[1973:f803] changePopoverSize-----end

2012-08-17 17:14:11.701 awrbv[1973:f803] changeText:(NSString *)string

如果要想多线程的话,可以是使用

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

或者

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

 

代码如下:

 

- (void)changeText:(NSString *)string

{

    label.text = string;

    NSLog(@"changeText:(NSString *)string");

}

 

- (void)changePopoverSize

{

    [self performSelectorOnMainThread:@selector(changeText:) withObject:@"Happy aha111" waitUntilDone:YES];

    NSLog(@"changePopoverSize#####end");

    sleep(5);

    NSLog(@"changePopoverSize-----end");

}

执行结果如下:

 

2012-08-17 17:19:29.618 awrbv[2024:f803] changeText:(NSString *)string

2012-08-17 17:19:29.619 awrbv[2024:f803] changePopoverSize#####end

2012-08-17 17:19:34.620 awrbv[2024:f803] changePopoverSize-----end

可以看出,如果waitUntilDone:YES那么等changeText执行完毕后再往下执行

如果waitUntilDone:NO的话,结果如下:

 

2012-08-17 17:21:12.135 awrbv[2049:f803] changePopoverSize#####end

2012-08-17 17:21:17.137 awrbv[2049:f803] changePopoverSize-----end

2012-08-17 17:21:17.139 awrbv[2049:f803] changeText:(NSString *)string

你可能感兴趣的:(Objective-c)