Ios 未使用dispatch_async和重复NSTimer调用方法

Ios 未使用dispatch_async和重复NSTimer调用方法,ios,iphone,grand-central-dispatch,nstimer,Ios,Iphone,Grand Central Dispatch,Nstimer,我正在开发一个应用程序,我想在其中使用dispatch\u async调用单独队列中的方法。我想在一定时间间隔后重复调用该方法。但是该方法没有被调用 我不知道怎么了。这是我的密码: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0

我正在开发一个应用程序,我想在其中使用
dispatch\u async
调用单独队列中的方法。我想在一定时间间隔后重复调用该方法。但是该方法没有被调用

我不知道怎么了。这是我的密码:

dispatch_async( NotificationQueue, ^{

        NSLog(@"inside queue");
        timer = [NSTimer scheduledTimerWithTimeInterval: 20.0
                                                 target: self
                                               selector: @selector(gettingNotification)
                                               userInfo: nil
                                                repeats: YES];

        dispatch_async( dispatch_get_main_queue(), ^{
            // Add code here to update the UI/send notifications based on the
            // results of the background processing

        });
    });

-(void)gettingNotification {
    NSLog(@"calling method ");
}
寻找更多

试试这段代码

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    dispatch_async( dispatch_get_main_queue(), ^{

        timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
                                               selector: @selector(gettingNotification) userInfo: nil repeats: YES];
        // Add code here to update the UI/send notifications based on the
        // results of the background processing

    });
});

-(void)gettingNotification {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         //background task here
    dispatch_async( dispatch_get_main_queue(), ^{
        // update UI here
        );
});
}

如果要在
调度队列上调用重复计时器,请与以下命令一起使用:

这将创建一个计时器,该计时器每20秒运行一次(第3个参数至),具有1秒的回旋余地(第4个参数至
调度\u源\u设置\u计时器

要取消此计时器,请使用:


但我将如何重复调用该方法这也是正确的,尽管分派源可能是更直接的机制。不过,这个答案可以简化,因为没有必要将计时器的创建分配到后台队列,然后再分配到主队列。只需在主队列中创建重复计时器,而不需要两个嵌套的调度。尽管如此,+1.仅将上述内容添加到vc中加载的视图中并不会生成任何控制台输出,缺少什么?@davidkarsson
timer
变量实际上必须是类属性(或ivar)。与
NSTiner
不同,如果它不在范围内,调度计时器将被取消并解除分配。
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    dispatch_async( dispatch_get_main_queue(), ^{

        timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
                                               selector: @selector(gettingNotification) userInfo: nil repeats: YES];
        // Add code here to update the UI/send notifications based on the
        // results of the background processing

    });
});

-(void)gettingNotification {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         //background task here
    dispatch_async( dispatch_get_main_queue(), ^{
        // update UI here
        );
});
}
dispatch_queue_t  queue = dispatch_queue_create("com.firm.app.timer", 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 20ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);

dispatch_source_set_event_handler(timer, ^{

    // stuff performed on background queue goes here

    NSLog(@"done on custom background queue");

    // if you need to also do any UI updates, synchronize model updates,
    // or the like, dispatch that back to the main queue:

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"done on main queue");
    });
});

dispatch_resume(timer);
dispatch_source_cancel(timer);