Chain Blocks in a Queue (with MKBlockQueue)
MKBlockQueue allows you to create a chain of blocks and execute them one after the other in a queue. Compared with NSOperation, with MKBlockQueue you decide yourself when a block is complete and when you want the queue to continue. You can also pass data from one block to the next.
https://github.com/MKGitHub/MKBlockQueue
Example Code
Section titled “Example Code”// create the dictionary that will be sent to the blocksvar myDictionary:Dictionary<String, Any> = Dictionary<String, Any>()myDictionary["InitialKey"] = "InitialValue"
// create block queuelet myBlockQueue:MKBlockQueue = MKBlockQueue()
// block 1let b1:MKBlockQueueBlockType ={ (blockQueueObserver:MKBlockQueueObserver, dictionary:inout Dictionary<String, Any>) in
print("Block 1 started with dictionary: \(dictionary)") dictionary["Block1Key"] = "Block1Value"
// tell this block is now completed blockQueueObserver.blockCompleted(with:&dictionary)
}
// block 2let b2:MKBlockQueueBlockType ={ (blockQueueObserver:MKBlockQueueObserver, dictionary:inout Dictionary<String, Any>) in
var copyOfDictionary:Dictionary<String, Any> = dictionary
// test calling on main thread, async, with delay DispatchQueue.main.asyncAfter(deadline:(.now() + .seconds(1)), execute: { print("Block 2 started with dictionary: \(copyOfDictionary)")
copyOfDictionary["Block2Key"] = "Block2Value"
// tell this block is now completed blockQueueObserver.blockCompleted(with:©OfDictionary) })}
// block 3let b3:MKBlockQueueBlockType ={ (blockQueueObserver:MKBlockQueueObserver, dictionary:inout Dictionary<String, Any>) in
var copyOfDictionary:Dictionary<String, Any> = dictionary
// test calling on global background queue, async, with delay DispatchQueue.global(qos:.background).asyncAfter(deadline:(.now() + .seconds(1)), execute: { print("Block 3 started with dictionary: \(copyOfDictionary)")
copyOfDictionary["Block3Key"] = "Block3Value"
// tell this block is now completed blockQueueObserver.blockCompleted(with:©OfDictionary) })}
// add blocks to the queuemyBlockQueue.addBlock(b1)myBlockQueue.addBlock(b2)myBlockQueue.addBlock(b3)
// add queue completion block for the queuemyBlockQueue.queueCompletedBlock({ (dictionary:Dictionary<String, Any>) in print("Queue completed with dictionary: \(dictionary)")})
// run queueprint("Queue starting with dictionary: \(myDictionary)")myBlockQueue.run(with:&myDictionary)
