Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling UICollectionView blocks main thread

I have a video decoder playing H264 using AVSampleBufferDisplayLayer and all works well until I scroll a UICollectionViewController on the same View Controller. This appears to block the main thread causing the app to crash. I have tried putting this code in a block on a separate queue using dispatch_async but still have the same blocking problem along with further performance issues on the decoder.

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)");
                        }
                    });

Is there a way to give this task priority over User Interface actions like scrolling a Collection View etc? Apologies for lack of understanding I am reasonably new to IOS.

like image 568
Md1079 Avatar asked Nov 01 '22 16:11

Md1079


1 Answers

Turns out the UICollectionView was blocking the delegate calls for NSURLConnection on the main thread. This solved the problem:

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

changed to

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                          delegate:self
                                                  startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
                  forMode:NSRunLoopCommonModes];
[connection start];
like image 110
Md1079 Avatar answered Nov 15 '22 05:11

Md1079