如何在响应用户交互时运行重复的NSTimer或计划进程

pet*_*ine 2 background-process nstimer ios

我用的时候

[NSTimer scheduledTimerWithTimeInterval:intervalCountDownTimer target:self selector:@selector(updateCountDownDurationForTimer:) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

如果我滚动一个,它会暂停UITableView.只有在滚动停止后才会恢复.

我的tableView用于dispatch_async & dispatch_sync异步加载图像,我不确定这是否是主要原因.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            FXDMediaItem *mediaItem = [self mediaItemAtIndexPath:indexPath];

            if (mediaItem) {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    cell.imageviewAlbumArt.image = [mediaItem albumArtImage];
                });
            }
        });
Run Code Online (Sandbox Code Playgroud)

我一直在寻找在后台模式下运行此计时器的方法,但找不到合适的解决方案.它不一定是NSTimer,但我需要使用预定的重复过程.

我怀疑答案可能实际上是一个简单的答案,但找不到它

ian*_*ian 6

这里的问题是通过使用scheduledTimerWithTimeInterval创建计时器:target:selector:userInfo:repeats:在默认模式下将其添加到主运行循环中.滚动UITableView(实际上是UIScrollView)时,这似乎暂停了.这里的技巧是使用不同的模式将您的计时器添加到主运行循环,如下所示:

timer = [NSTimer timerWithTimeInterval:intervalCountDownTimer target:self selector:@selector(updateCountDownDurationForTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)