NSTimer scheduleTimerWithTimeInterval:和performSelectorOnMainThread

时间:2011-03-09 14:22:04

标签: iphone objective-c multithreading cocoa-touch ios4

在我的iPhone应用程序中,我的代码如下:

[NSTimer scheduledTimerWithTimeInterval:03.45  
                                 target:self 
                               selector:@selector(animatePickerTimer:) 
                                userInfo:picker 
                                 repeats:NO];  

[NSTimer scheduledTimerWithTimeInterval:04.15 
                                 target:self 
                               selector:@selector(animatePickerTimer1:) 
                               userInfo:picker 
                                repeats:NO];  

[NSTimer scheduledTimerWithTimeInterval:04.91 
                                 target:self 
                               selector:@selector(animatePickerTimer2:) 
                               userInfo:picker 
                                repeats:NO]; 


-(void) animatePickerTimer: (NSTimer*) timer{
       [self performSelectorOnMainThread:@selector(animatePicker:) 
                              withObject:(UIPickerView *)timer.userInfo 
                           waitUntilDone:NO];
}

-(void) animatePickerTimer1: (NSTimer*) timer{
       [self performSelectorOnMainThread:@selector(animatePicker1:) 
                              withObject:(UIPickerView *)timer.userInfo 
                           waitUntilDone:NO];
}

-(void) animatePickerTime2r: (NSTimer*) timer{
       [self performSelectorOnMainThread:@selector(animatePicker2:) 
                              withObject:(UIPickerView *)timer.userInfo 
                           waitUntilDone:NO];
}

在一段时间后调用线程。 我希望在完成所有三个线程后调用一个方法。我怎么知道线程是否完成?

1 个答案:

答案 0 :(得分:2)

实际上有两种方法可以做到这一点:

  1. 让每个回调方法调用第二个辅助方法,该方法只计算它被调用的次数。一旦对该方法的调用次数等于您拥有的定时器数量,就完成了。
  2. 进行第二次回调,但不是简单地计算计时器,而是存储每个NSTimer实例并让助手方法在每个计时器上检查isValid。一旦所有的计时器都无效(即不再能够解雇),你就完成了。
  3. 在代码中,这些方法可能类似于:

    NSInteger timersReceived = 0;
    
    NSTimer * timer1 = [NSTimer scheduledTimerWithTimeInterval:03.45 target:self selector:@selector(animatePickerTimer:) userInfo:picker repeats:NO];  
    NSTimer * timer2 = [NSTimer scheduledTimerWithTimeInterval:04.15 target:self selector:@selector(animatePickerTimer1:) userInfo:picker repeats:NO];  
    NSTimer * timer3 = [NSTimer scheduledTimerWithTimeInterval:04.91 target:self selector:@selector(animatePickerTimer2:) userInfo:picker repeats:NO]; 
    
    
    -(void) animatePickerTimer: (NSTimer*) timer{
           [self performSelectorOnMainThread:@selector(animatePicker:) withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           [self receivedTimer:timer];
    }
    
    -(void) animatePickerTimer1: (NSTimer*) timer{
           [self performSelectorOnMainThread:@selector(animatePicker1:) withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           [self receivedTimer:timer];
    }
    
    -(void) animatePickerTime2r: (NSTimer*) timer{
           [self performSelectorOnMainThread:@selector(animatePicker2:) withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           [self receivedTimer:timer];
    }
    
    // Method 1
    -(void) receivedTimer: (NSTimer*) timer{
           timersReceived++;
           if(timersReceived == 3) {
                  [self callMethodAfterCompletionOfAllTimers];
           }
    }
    
    // Method 2
    - (void) receivedTimer: (NSTimer*) timer{
           if(![timer1 isValid] && ![timer2 isValid] && ![timer3 isValid]) {
                  [self callMethodAfterCompletionOfAllTimers];
           }
    }

    请注意,如果您使用方法2,实际上可以将所有这些方法折叠为您从所有三个计时器调用的单个方法,因为您保留了计时器句柄:

    NSTimer * timer1 = [NSTimer scheduledTimerWithTimeInterval:03.45 target:self selector:@selector(receivedTimer:) userInfo:picker repeats:NO];  
    NSTimer * timer2 = [NSTimer scheduledTimerWithTimeInterval:04.15 target:self selector:@selector(receivedTimer:) userInfo:picker repeats:NO];  
    NSTimer * timer3 = [NSTimer scheduledTimerWithTimeInterval:04.91 target:self selector:@selector(receivedTimer:) userInfo:picker repeats:NO]; 
    
    - (void)receivedTimer:(NSTimer *)timer {
           if(timer == timer1) {
                  [self performSelectorOnMainThread:@selector(animatePicker:) withObject:withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           } else if(timer == timer2) {
                  [self performSelectorOnMainThread:@selector(animatePicker1:) withObject:withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           } else if(timer == timer3) {
                  [self performSelectorOnMainThread:@selector(animatePicker2:) withObject:withObject:(UIPickerView *)timer.userInfo waitUntilDone:NO];
           }
    
           if(![timer1 isValid] && ![timer2 isValid] && ![timer3 isValid]) {
                  [self callMethodAfterCompletionOfAllTimers];
           }
    }

    这种组合方法更符合Apple用于(例如)键值观察和某些委托协议的策略。