在我的应用程序中,我需要处理一组数据并仅在互联网连接可用时将其发送到服务器。我需要在单独的线程上执行此任务,以便应用程序的正常执行不会中断。
我创建了一个名为 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];
}
我还尝试在[process processQueue]'方法中使用performSelector而不是
dispatch_async`,但不起作用。
最佳答案
试试这个..
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];
});
关于iphone - 在单独的 iOS 线程上延迟执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625134/