Search code examples
iosuitableviewswift2nsoperationnsoperationqueue

NSOperationQueue per row of UITableView


I need to launch several NSOperations to load a table with videos from AWS S3 (two operations per row of the table), but after a row in a table is selected, I want to pass those operations associated with that row of the table to the next view controller to continue downloads and download other videos. I would then suspend the other operations from the other rows of the table, and resume them when I return to the table view controller.

Is it safe to have an NSOperationQueue for each row of a UITableView? Is there a better way to accomplish what I want?


Solution

  • I'd definitely prefer having a separate (and preferably shared) manager (let's call it VideosManager, for example) with exported (maybe indirect) access to operations, separate queue(s) management, thread-safety + locking stuff etc. instead of any solution depending on controllers hierarchy.

    You can stick your approach with the view controllers model, but it's not a good idea in most cases.

    Quick sample API to design and implement:

    (assume you have a Video interface implemented with further info)

    @interface VideoManager
    
    + (instancetype)sharedManager; // returning a shared singleton
    
    - (void)startDownloadOfVideo:(Video *)video;
    - (void)cancelDownloadOfVideo:(Video *)video;
    - (void)cancelDownloadsExceptForVideo:(Video *)video;
    - (BOOL)isDownloadInProgressForVideo:(Video *)video;
    
    @end
    

    The implementation would handle and operate download operations in required queues (mainly connection instance and its delegate queue planning), handle incoming data, properly write it to a file or output stream, allowing you to dynanically control the flow depending on your needs. This is only a quick naive scheme you can iterate on and make it fit your requirements.