mirror of
https://github.com/samsonjs/Advanced-NSOperations.git
synced 2026-03-25 08:25:47 +00:00
62 lines
1.9 KiB
Swift
62 lines
1.9 KiB
Swift
/*
|
||
Copyright (C) 2015 Apple Inc. All Rights Reserved.
|
||
See LICENSE.txt for this sample’s licensing information
|
||
|
||
Abstract:
|
||
This code shows how to create a simple subclass of EarthquakeOperation.
|
||
*/
|
||
|
||
import Foundation
|
||
|
||
/// A closure type that takes a closure as its parameter.
|
||
typealias OperationBlock = (@escaping () -> Void) -> Void
|
||
|
||
@available(*, deprecated, message: "Use () -> Void directly, it's shorter")
|
||
typealias dispatch_block_t = () -> Void
|
||
|
||
/// A sublcass of `EarthquakeOperation` to execute a closure.
|
||
class EarthquakeBlockOperation: EarthquakeOperation {
|
||
private let block: OperationBlock?
|
||
|
||
/**
|
||
The designated initializer.
|
||
|
||
- parameter block: The closure to run when the operation executes. This
|
||
closure will be run on an arbitrary queue. The parameter passed to the
|
||
block **MUST** be invoked by your code, or else the `BlockOperation`
|
||
will never finish executing. If this parameter is `nil`, the operation
|
||
will immediately finish.
|
||
*/
|
||
init(block: OperationBlock? = nil) {
|
||
self.block = block
|
||
super.init()
|
||
}
|
||
|
||
/**
|
||
A convenience initializer to execute a block on the main queue.
|
||
|
||
- parameter mainQueueBlock: The block to execute on the main queue. Note
|
||
that this block does not have a "continuation" block to execute (unlike
|
||
the designated initializer). The operation will be automatically ended
|
||
after the `mainQueueBlock` is executed.
|
||
*/
|
||
convenience init(mainQueueBlock: @escaping () -> Void) {
|
||
self.init(block: { continuation in
|
||
DispatchQueue.main.async {
|
||
mainQueueBlock()
|
||
continuation()
|
||
}
|
||
})
|
||
}
|
||
|
||
override func execute() {
|
||
guard let block = block else {
|
||
finish()
|
||
return
|
||
}
|
||
|
||
block {
|
||
self.finish()
|
||
}
|
||
}
|
||
}
|