0

Need to unzip 10 files in document directory for that i use dispatch async like this

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,   0), ^{
 // unzip 5 files

})
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// unzip another 5 files

})

my doubt is will it do the unzip concurrently?

If is it so, while first 5 files getting unzip, another 5 files also getting unzipped at the same time ?

how can i do it efficiently ?

any help would be appreciable.

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
  • By the way, nowadays, you'd use a Quality of Service value (e.g. `QOS_CLASS_UTILITY`) rather than `DISPATCH_QUEUE_PRIORITY_HIGH`. – Rob Jul 21 '16 at 10:08

3 Answers3

0

If you unzip total files it will be good.Because It is asynchronous.While doing background operation simultaneously if you try to do other background process,it makes problem.

DISPATCH_QUEUE_PRIORITY_HIGH Items dispatched to the queue will run at high priority, i.e. the queue will be scheduled for execution before any default priority or low priority queue.

If you want to run a single independent queued operation and you’re not concerned with other concurrent operations, you can use the global concurrent queue

dispatch_queue_t globalConcurrentQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

It runs asynchronously on a background thread. This is done because parsing data may be a time consuming task and it could block the main thread which would stop all animations and the application wouldn't be responsive.

Grand Central Dispatch

dispatch_async

Asynchronous Operations

Apple Document

UI and main Thread

GLobal Queue

dispatch_sync and dispatch_async Process

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Because global queues are concurrent queues, your second dispatch_async will happen concurrently with the first one. If you do this, you must ensure that:

  • The unzip class/instance is thread safe;
  • All model updates must be synchronized; and
  • The peak memory usage resulting from simultaneous zip operations isn't too high.

If the above conditions aren't satisfied (i.e. if you want to run these zip tasks consecutively, not concurrently), you can create a serial queue for unzipping (with dispatch_queue_create). That way, any unzipping tasks dispatched to that queue will be performed serially on a background queue, preventing them from running concurrently.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

Calling dispatch_get_global_queue returns a concurrent, background queue (ie a queue capable of running more than on queue item at once on a background thread) that is managed by the OS. The concurrency of the operations really depends on the number of cores the device has.

Each block you pass to dispatch_async is a queue item, so the code inside the block will run linearly on the background thread when that block is dequeued and run. If for example you are unzipping with a for loop, as such:

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
     for (int i = 0; i<5; i++) {
        // Unzip here
     }

}) 

Then the task running time will be 5 x file unzip time. Batching them in to two sets of 5 could potentially mean the total unzip time is halved.

If you want all 10 files unzipped with max concurrency (ie as much concurrency as the system will allow), then you'd be better off dispatching 10 blocks to the global_queue, as such:

for (int i = 0; i<5; i++) {

       dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        // Unzip here
    }) 
}
Sam Clewlow
  • 4,293
  • 26
  • 36