18

I read Apple documentation on how to Use serial queues to ensure that tasks to execute in a predictable order but now i am confused too much.
Some how i am able to work serially but still i am not clear so i need simple serial example for my methods to execute serially.

I divided my functionality in to 4 parts and now want them to execute Serially

[self ReadAllImagesFromPhotosLibrary];

[self WriteFewImagestoDirectory];

[self GettingBackAllImagesFromFolder]; 

[self MoveToNextView];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
DreamWatcher
  • 399
  • 1
  • 3
  • 11

6 Answers6

42

To follow-up and improve iCoder's answer, you could and should do the following.

dispatch_queue_t serialQueue = dispatch_queue_create("com.unique.name.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
        [self ReadAllImagesFromPhotosLibrary];
    }); 
dispatch_async(serialQueue, ^{
         [self WriteFewImagestoDirectory];
});
dispatch_async(serialQueue, ^{
    [self GettingBackAllImagesFromFolder]; 
});
dispatch_async(serialQueue, ^{
    [self MoveToNextView];
});

Despite the above calls being async, they will be queued and run serially as the DISPATCH_QUEUE_SERIAL states. The difference between sync and async is that with sync, your code will pause and wait for the block answer before running the following code, thus potentially freezing your UI if the execution time is long. Whereas with async, the code runs on and the block is returned asynchronously.

However, the tasks you have stored in the DISPATCH_QUEUE_SERIAL will wait and be executed one after the other in the order they were added, thanks to GCD (Grand Central Dispatch).

Community
  • 1
  • 1
Benjamin
  • 8,128
  • 3
  • 34
  • 45
5
dispatch_queue_t serialQueue = dispatch_queue_create("com.unique.name.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
        [self ReadAllImagesFromPhotosLibrary];
             dispatch_async(serialQueue, ^{
                     [self WriteFewImagestoDirectory];
                     dispatch_async(serialQueue, ^{
                         [self GettingBackAllImagesFromFolder]; 
                         dispatch_async(serialQueue, ^{
                              [self MoveToNextView];
                         });
                   });
              });
    }); 

I think the above code should work, but make sure the UI operations are executed in the main thread. Hope it helps.

iCoder
  • 1,645
  • 1
  • 15
  • 23
1

You can use NSOperationQueue with maxConcurrentOperationCount set to 1 (or even set dependency for each NSOperation, so it won't start before its dependency is finished).

Here is NSOperationQueue Class Reference.

Also take a look at this question.

Community
  • 1
  • 1
Dmitry Zhukov
  • 1,809
  • 2
  • 27
  • 35
1

I am not much aware of existing API for doing the same with blocks, if any.

But the same can be done by defining blocks(representing the operations you want) in a fashion that they point to next block to proceed if any. Also, you can put the whole processing in a separate queue.

snippet for having blocks executing in serial fashion

BLOCK A(NEXT BLOCK reference){  
->Do the the required Task  
->If(next Block reference)  
--->Then call that block 
->Else  
--->Exit or have a callback on mainthread   
}  
ajonnet
  • 215
  • 2
  • 9
0

why not try the GCD, it guarantees the sequence of operation and also has sync and async capabilities

-2

I had some success with a pattern like this in a similar hunt in Swift 3.0 ...

let serialQueue = DispatchQueue.init(label: "com.foo.bar")

serialQueue.sync {self.readAllImagesFromPhotosLibrary()}

serialQueue.sync {self.rriteFewImagestoDirectory()}

serialQueue.sync {self.gettingBackAllImagesFromFolder()}

serialQueue.sync {self.moveToNextView()}
Ajean
  • 5,528
  • 14
  • 46
  • 69
David Reid
  • 31
  • 1
  • 1
  • 6