PerformSelector延迟不在后台模式下运行 - iPhone

Ida*_*dan 10 iphone objective-c nstimer nsrunloop ios4

我有一个voip应用程序,它也在后台运行.当我在后台时,我正在从主线程调用:(在我诊断网络丢失的情况下建立网络连接).

[self performSelector :@selector(Reconnect:) withObject:nil afterDelay:60.0];
Run Code Online (Sandbox Code Playgroud)

但是,只有当我的应用程序返回到前台时才会执行选择器.我应该做些什么来让选择器在后台执行吗?

谢谢

编辑:

-(void) reconectInBackgroundAfterDelay:(NSTimeInterval) dealy
{
    NSLog(@"reconectInBackgroundAfterDelay");
    UIApplication*   app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [self performSelector :@selector(Reconnect:) withObject:nil afterDelay:dealy];

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}
Run Code Online (Sandbox Code Playgroud)

我添加了这个代码,但仍然没有在提供的延迟后调用"重新连接"方法.我已经在后台调用"reconectInBackgroundAfterDelay"方法.

还有其他建议吗?

编辑2 找到解决方案.见下文

Ida*_*dan 22

到目前为止我找到的唯一解决方案:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

        NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(Reconnect:) userInfo:nil repeats:NO];    

        [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

        [[NSRunLoop currentRunLoop] run]; 
    }); 
Run Code Online (Sandbox Code Playgroud)