iphone - 在单独的 iOS 线程上延迟执行

标签 iphone ios multithreading

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

我创建了一个名为 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/

相关文章:

ios - 检查字符串是否具有有效数字

ios - FBSDKAppEvents.activateApp() 无法识别的选择器发送到实例

python - 高效矩阵 vector 乘法 : Multithreading directly in Python vs. 使用 ctypes 绑定(bind)多线程 C 函数

c++ - 原子int集合的线程安全

java - 如何将Guava缓存发送到另一种方法时每30秒清空一次?

ios - UIScrollView iOS 应用中的动画

ios - 在 iPhone 上播放歌曲时通过麦克风检测噪音

ios - 在自定义 UITableViewCell 中调整 UILabel 大小的问题

iphone - 仅显示几张图像,滚动不顺畅

ios - 黑色条纹显示在旧项目的 View 中