Last active
August 29, 2015 14:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class DispatchGroup { | |
private var blocks: [dispatch_block_t] = [] | |
private let dispatch_queue = dispatch_queue_create("com.dispatch_group.foo", DISPATCH_QUEUE_CONCURRENT) | |
private let dispatch_group = dispatch_group_create() | |
func andThen(block: dispatch_block_t) -> Group { | |
blocks += [block] | |
return self | |
} | |
func noAndThen(completion: dispatch_block_t) -> Group { | |
for block in blocks { | |
dispatch_group_async(dispatch_group, dispatch_queue, block) | |
} | |
dispatch_group_notify(dispatch_group, dispatch_queue, completion) | |
return self | |
} | |
} | |
let foo = Group() | |
for i in 1...10 { | |
foo.andThen { println(i) } | |
} | |
foo.andThen { | |
println("foo") | |
}.andThen { | |
println("bar") | |
}.andThen { | |
println("baz") | |
}.noAndThen { | |
println("completion") | |
} | |
// ❯ ./group.swift | |
// 51426 | |
// | |
// 3 | |
// 7 | |
// | |
// 891 | |
// | |
// fbb | |
// | |
// 0oaa | |
// orz | |
// | |
// | |
// completion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment