2012-09-07 58 views
0
-(void)manageNetConnection{ 
    static BOOL closing=FALSE; 
    NSLog(@"connecting Imap after net"); 
    if([imapStoreObj isStoreConnected] && closing==FALSE){ 
     [imapStoreObj close]; 
     NSLog(@"close store"); 
     closing=TRUE; 
     [self performSelector:@selector(manageNetConnection) withObject:nil afterDelay:5.0]; 

     return; 
    }else if ([imapStoreObj isStoreConnected] && closing==TRUE) { 
     [self performSelector:@selector(manageNetConnection) withObject:nil afterDelay:5.0]; 

     return; 
    } 
    closing=FALSE; 
    [indicatorForGetMail setHidden:NO]; 
    [indicatorForGetMail startAnimation:nil]; 
    netOff=2; 
    NSLog(@"netOff==%d",netOff); 

    [editFolderTable setAllowsMultipleSelection:NO]; 
    NSLog(@"connect net"); 

    [self reconnect]; 


} 

此函数预计会自行调用,直到连接重新建立。问题是该函数在指定的延迟后不会自行调用。 请帮忙延迟后执行选择器

+0

你有固定的这个问题? – yoninja

回答

0

我遇到了与@performSelector相同的问题。改用@performSelectorOnMainThread和NSTimer。

-(void)manageNetConnection{ 
    ... 
    [self performSelectorOnMainThread:@selector(startTimer:) withObject:@"manageNetConnection" waitUntilDone:YES]; 
    ... 
} 

- (void)startTimer:(NSString *)data{ 
    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(endTimer:) userInfo:data repeats:NO]; 
} 

- (void)endTimer:(NSTimer *)timer{ 
    NSString *target = [timer userInfo]; 
    SEL targetSelector = NSSelectorFromString(target); 
    [self performSelectorInBackground:targetSelector withObject:nil]; 
} 
0

如果你的两个条件都令人满意,下面的命令将会调用-(void)manageNetConnection;方法。

[self performSelector:@selector(manageNetConnection) withObject:nil afterDelay:5.0]; 

检查条件是否满足,条件是否满足。

if([imapStoreObj isStoreConnected] && closing==FALSE){ 

     [self performSelector:@selector(manageNetConnection) withObject:nil afterDelay:5.0]; 

     return; 
    }else if ([imapStoreObj isStoreConnected] && closing==TRUE) { 
     [self performSelector:@selector(manageNetConnection) withObject:nil afterDelay:5.0]; 

     return; 
    } 

否则你可以使用重复呼叫

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(testMethod:) userInfo:nil repeats:YES];