2

当我使用

[NSTimer scheduledTimerWithTimeInterval:intervalCountDownTimer target:self selector:@selector(updateCountDownDurationForTimer:) userInfo:nil repeats:YES];

如果我滚动一个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];
                });
            }
        });

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

我怀疑答案实际上可能很简单,但找不到

4

3 回答 3

6

这里的问题是,通过使用 scheduleTimerWithTimeInterval: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];
于 2012-07-02T08:27:19.827 回答
0

如果您已经在使用 GCD 进行后台工作,请尝试将里面的内容放在updateCountDownDurationForTimer:后台线程中。

于 2012-07-02T08:08:35.983 回答
0

至于你的计时器,你看过dispatch_source_create()吗?

我假设您正在更新标签或其他内容,对吗?在表格视图滚动时更新标签,在-scrollViewDidScroll:不滚动时保持计时器。

如果我的假设有误,请在计时器中发布一些您正在做什么的代码,我会相应地更新我的答案。

于 2012-07-02T08:22:09.630 回答