Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatch Group Serial Queue

I'm trying to understand what will be the correct way to create Serial Dispatch Group for background threaded tasks. Example:

I have a video which I want to split into 5 parts. So in theory this will be the queue cycle:

  • Run AVAssetExportSession on a background thread
  • Wait for it to finish
  • Notify finished
  • Run AVAssetExportSession on a background thread
  • And so on and on

I've been digging through tutorials regarding this issue, yet I couldn't find an appropriate way to achieve this.

Any help would be highly appricated!

Best Regards, Roi

like image 406
Roi Mulia Avatar asked Sep 12 '25 12:09

Roi Mulia


2 Answers

Synchronization via groups is useful if the tasks are to be processed in parallel and serially and the last task is unclear. But your problem is completely serial.

Why don't you use just a serial queue for your purpose? You can add the five blocks to that queue and they will be executed in the desired order. You might also solve your problem with just a single block running in background.

like image 163
clemens Avatar answered Sep 15 '25 02:09

clemens


There's no such thing as a "serial dispatch group". Serial/concurrent is a property of a queue, not of a group.

Given that AVAssetExportSession is, itself, an asynchronous process, simple attempts to add it to a serial queue will not work. Two approaches seem logical. You can either:

  1. You can wrap the exportAsynchronously(completionHandler:) in an asynchronous custom Operation subclass. You can then add these five operations to a serial Operation queue. You can give them individual completion blocks if you want. And/or you can then make a completion operation dependent on those five operations.

    or

  2. You can write a recursive function that performs request i, and in its completion handler, you can have it call itself for i + 1. And it just needs to check to see if i < 5 (or whatever).

like image 33
Rob Avatar answered Sep 15 '25 02:09

Rob