Load a video on a UICollectionViewCell with a background thread to create seamless scrolling Load a video on a UICollectionViewCell with a background thread to create seamless scrolling multithreading multithreading

Load a video on a UICollectionViewCell with a background thread to create seamless scrolling


You can define an “NSOperationQueue“ for running background operations and when cell is visible you can load video to cell of UICollectionView.

Also, don’t forget to take advantage of the NSOperationQueue and call “cancelAllOperations” when the collectionView is not needed anymore


Is there a way to edit this function and put it on a background thread so that instead of a a 4 second lag in scrolling, there is seamless, uninterrupted scrolling?

You are running an SDK as you mention:

Kolor Eyes is a free 360 video player app rendering spherical videos with pixel accurate projection algorithms. The user can choose different camera viewpoints anytime through touch gestures or device motion controls. More than a player, it's the perfect browser designed for Kolor 360 video hosting website. This documentation concerns Kolor Eyes iOS from version 2.0

It's enough problems running Apples own "AVPlayer" in a UICollectionView/UITableView without lags, and you want to run a Custom made 360 video player without any lags.

Since it is an SDK, you can not access or/and modify any of the code, you need to contact the developers of the SDK to get the guidelines. I don't think that this SDK is developed to be run in a scrollView in mind , since this seems to be heavy task (I am not sure 100% however, you need to ask the developers).

However, you should not modify the UI on a background thread as you are asking, that wont help the lags go away, it will make it worse and even crash or hang your application in most cases, and I am sure (hope so atleast) that the developers do as much threading as possible already with this SDK:

In a Cocoa application, the main thread runs the user interface, that is, all drawing and all events are handled on the main thread. If your application performs any lengthy synchronous operations on that thread, your user interface can become unresponsive and trigger the spinning cursor. To avoid this, you should shorten the amount of time consumed by those operations, defer their execution, or move them to secondary threads.

Source


In theory you are right. You should be able to do non-UI related video loading (such as downloading the video) on a background thread, and the dispatch to the main thread when you actually want to display it. In practice I have found this be very hard with many 3rd party libraries. Most video libraries expect to be run entirely on the main thread. You would have to check with your video sdk - but I would be surprised if they supported background loading.

Another solution is to not load the video in the cellForItemAtIndexPath:, but instead load the videos for the visible cell when scrolling stops. You can monitor the delegate callbacks scrollViewDidEndDragging and/or scrollViewDidEndDecelerating.