iphone - 在 TableView 中使用 GCD 和加载缩略图时遇到问题

标签 iphone ios cocoa-touch parallel-processing grand-central-dispatch

我有以下代码尝试在 TableView 中异步加载一行缩略图:

for (int i = 0; i < totalThumbnails; i++)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
    ^{
        __block GraphicView *graphicView;
        __block Graphic *graphic;

        dispatch_async(dispatch_get_main_queue(), 
        ^{
            graphicView = [[tableViewCell.contentView.subviews objectAtIndex:i] retain];
            graphic = [[self.thumbnailCache objectForKey: [NSNumber numberWithInt:startingThumbnailIndex + i]] retain];

            if (!graphic)
            {
                graphic = [[self graphicWithType:startingThumbnailIndex + i] retain];
                [self.thumbnailCache setObject: graphic forKey:[NSNumber numberWithInt:startingThumbnailIndex + i]];
            }

            [graphicView setGraphic:graphic maximumDimension:self.cellDimension];
        });

        [graphicView setNeedsDisplay];

        dispatch_async(dispatch_get_main_queue(), 
        ^{
            CGRect graphicViewFrame = graphicView.frame;
            graphicViewFrame.origin.x = ((self.cellDimension - graphicViewFrame.size.width) / 2) + (i * self.cellDimension);
            graphicViewFrame.origin.y = (self.cellDimension - graphicViewFrame.size.height) / 2;
            graphicView.frame = graphicViewFrame;
        });

        [graphicView release];
        [graphic release];
    });

}

但是,当我运行代码时,我无法访问这一行:[graphicView setNeedsDisplay]; 值得一提的是,当我像这样设置代码时,代码工作正常:

for (int i = 0; i < totalThumbnails; i++)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
    ^{
        dispatch_async(dispatch_get_main_queue(), 
        ^{
              //put all the code here
        });
}

它工作正常,UITableView 在首次调用时异步加载,但滚动仍然非常不稳定。

所以我想让第一段代码开始工作,这样我就可以在全局线程而不是主线程中完成绘图(我假设这会解决滚动不连贯的问题?)。

由于 iOS4 绘图可以异步完成,所以我认为这不是问题所在。可能我滥用了 __Block 类型?

有人知道我怎样才能让它工作吗?

最佳答案

您完全误解了如何使用 GCD。查看您的代码:

        __block GraphicView *graphicView;

这里的变量没有初始化为 nil。向其发送消息是不安全的。

        __block Graphic *graphic;

        dispatch_async(dispatch_get_main_queue(), 
        ^{
            //statements
        });

此处的调度语句会立即返回。该系统为您工作,将此任务分拆到另一个线程上。在执行上述语句之前,或者可能同时执行,我们在此处继续执行下一行...

        [graphicView setNeedsDisplay];

此时图形 View 可能已经或可能没有被上面的调度语句初始化。很可能不会,因为没有时间。由于它仍未初始化,它指向随机内存,因此尝试向它发送消息会导致 EXC_BAD_ACCESS。

如果您想异步绘制单元格内容(或预渲染图像或其他。)我完全推荐观看 WWDC 2012 session 211 “在 iOS 上构建并发用户界面”。他们所做的几乎与您似乎正在尝试做的完全一样,并解释了您可能遇到的所有陷阱。

关于iphone - 在 TableView 中使用 GCD 和加载缩略图时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13135537/

相关文章:

iphone - Google Drive SDK - 流式传输音频文件

ios - “Unrecognized selector sent to instance” swift

ios - 在 Xcode 6 中使用自动布局时如何阻止图像自动调整大小?

iphone - 组合数组中的对象

ios - 为什么我会得到一个带有这个 NSMutableArray 代码的 NSFastEnumerationMutationHandler?

objective-c - UIAlert View Objective C - 打开应用商店链接

iphone - 将注释的标题设置为当前地址

ios - 如何使用 Facebook 图形 API 识别 Facebook 帖子是否已被点赞?

iphone - 将变量发送到添加的 subview View Controller

ios - 提交 Crashlytics : Unable to submit build - iOS app Jenkins