滚动UICollectionView阻止主线程

时间:2014-10-06 11:36:03

标签: ios grand-central-dispatch

我有一个使用AVSampleBufferDisplayLayer播放H264的视频解码器,一切正常,直到我在同一个View Controller上滚动UICollectionViewController。这似乎阻止导致应用程序崩溃的主线程。我已尝试使用dispatch_async将此代码放在一个单独的队列中的块中,但仍然具有相同的阻塞问题以及解码器上的其他性能问题。

dispatch_async(sampleQueue, ^{

                        [sampleBufferQueue addObject:(__bridge id)(sampleBuffer)];

                        if ([avLayer isReadyForMoreMediaData]) {
                            CMSampleBufferRef buffer = (__bridge CMSampleBufferRef)([sampleBufferQueue objectAtIndex:0]);
                            [sampleBufferQueue removeObjectAtIndex:0];
                            [avLayer enqueueSampleBuffer:buffer];
                            buffer = NULL;

                            NSLog(@"I Frame");
                            [avLayer setNeedsDisplay];
                            while ([sampleBufferQueue count] > 0 && [avLayer isReadyForMoreMediaData]) {

                                CMSampleBufferRef buffer = (__bridge CMSampleBufferRef)([sampleBufferQueue objectAtIndex:0]);
                                [sampleBufferQueue removeObjectAtIndex:0];
                                [avLayer enqueueSampleBuffer:buffer];
                                buffer = NULL;
                                NSLog(@"I Frame from buffer");
                                [avLayer setNeedsDisplay];
                            }
                        }
                        else {
                            NSLog(@"AVlayer Not Accepting Data (I)");
                        }
                    });

有没有办法让此任务优先于用户界面操作,例如滚动集合视图等?抱歉缺乏理解我对IOS来说是个新手。

1 个答案:

答案 0 :(得分:2)

结果是UICollectionView阻塞了主线程上NSURLConnection的委托调用。这解决了这个问题:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                          delegate:self];

更改为

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                          delegate:self
                                                  startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
                  forMode:NSRunLoopCommonModes];
[connection start];