Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from ChimeHQ/feature/queue-add-async
Browse files Browse the repository at this point in the history
NSOperationQueue addAsyncOperation
  • Loading branch information
mattmassicotte authored Feb 7, 2019
2 parents 44b435d + dd00fb8 commit 8e78a2c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions OperationPlus/AsyncBlockOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import Foundation

public class AsyncBlockOperation: AsyncOperation {
private let block: (@escaping () -> Void) -> Void
public typealias CompletionHandler = (@escaping () -> Void) -> Void

public init(block: @escaping (@escaping () -> Void) -> Void) {
private let block: CompletionHandler

public init(block: @escaping CompletionHandler) {
self.block = block
}

Expand Down
4 changes: 4 additions & 0 deletions OperationPlus/Extensions/OperationQueue+Enqueuing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ extension OperationQueue {
public func addOperations(_ ops: [Operation]) {
addOperations(ops, waitUntilFinished: false)
}

public func addAsyncOperation(block: @escaping AsyncBlockOperation.CompletionHandler) {
addOperation(AsyncBlockOperation(block: block))
}
}
15 changes: 15 additions & 0 deletions OperationPlusTests/OperationQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,19 @@ class OperationQueueTests: XCTestCase {

wait(for: [expectation], timeout: 1.0)
}

func testAddAsyncBlock() {
let queue = OperationQueue.serialQueue()

let blockExpecation = XCTestExpectation(description: "Block Expectation")

queue.addAsyncOperation { (completionHandler) in
blockExpecation.fulfill()
completionHandler()
}

let nextOpExpectation = OperationExpectation(operation: Operation(), queue: queue)

wait(for: [blockExpecation, nextOpExpectation], timeout: 1.0, enforceOrder: true)
}
}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ queue.currentOperationsFinished {
}
```

Convenient inline functions:

```swift
queue.addAsyncOperation { (completionHandler) in
DispatchQueue.global().async {
// do some async work
completionHandler()
}
}
```

### XCTest Support

**OperationTestingPlus** is an optional micro-framework to help make your XCTest-based tests a little nicer. When using Carthage, it is built as a static framework to help ease integration with your testing targets.
Expand Down

0 comments on commit 8e78a2c

Please sign in to comment.