2012-07-24 128 views
0

在我的应用程序中,我需要处理一组数据,并只有在互联网连接可用时才将其发送到服务器。我需要在单独的线程上执行此任务,以便不中断应用程序的正常执行。在独立的iOS线程上执行延迟

我创建了一个名为ProcessQueue的类,它遍历数组并将其发送到服务器。

我需要在ApplicationDidBecomeActive事件上执行这个任务,延迟几秒钟,但在一个单独的线程上。

我尝试过,但选择器只是不被调用。 (我试图在ProcessQueue类中设置断点)。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    [self performSelector:@selector(processTheQueue) withObject:nil afterDelay:5]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 

    }); 
}); 


- (void) processTheQueue 
{ 

    QueueProcessor *process = [[QueueProcessor alloc] init]; 
    [process processQueue]; 

} 

我也曾尝试使用performSelector的[process processQueue]' method instead of dispatch_async`内,但不起作用。

+0

人谁可以帮助? – tGilani 2012-08-06 10:58:18

回答

1

试试这个..

double delayInSeconds = 2.0; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     // code to be executed on main thread.If you want to run in another thread, create other queue 
     [self repeateThreadForSpecificInterval]; 
    });