延迟后使用performSelector重复发生

时间:2012-09-07 10:22:06

标签: objective-c xcode cocoa

-(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];


}

在重新建立连接之前,应该调用此函数。问题是该函数在指定的延迟后不会调用自身。 请帮忙

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

如果您的两个条件都令人满意,则下划线将在5.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];