Simple GCD Serial Queue example like FIFO using bl

2020-02-17 09:53发布

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];

6条回答
老娘就宠你
2楼-- · 2020-02-17 10:01

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).

查看更多
Explosion°爆炸
3楼-- · 2020-02-17 10:01

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   
}  
查看更多
手持菜刀,她持情操
4楼-- · 2020-02-17 10:07

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()}
查看更多
Rolldiameter
5楼-- · 2020-02-17 10:08
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.

查看更多
够拽才男人
6楼-- · 2020-02-17 10:08

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

查看更多
姐就是有狂的资本
7楼-- · 2020-02-17 10:21

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.

查看更多
登录 后发表回答