Scrolling UICollectionView blocks the main thread

I have a video decoder playing H264 using AVSampleBufferDisplayLayer and everything works fine until I loop through the UICollectionViewController on the same view controller. This seems to block the main thread causing the application to crash. I tried to put this code in a block in a separate queue using dispatch_async, but still have the same locking problem as the additional 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 any way to give this priority tasks over user interface actions, such as scrolling through a collection, etc.? Apologies for the lack of understanding. I am new to iOS.

+5
source share
1 answer

It turns out that UICollectionView blocks delegate calls for NSURLConnection in 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]; 
+2
source

Source: https://habr.com/ru/post/1204099/


All Articles