Features • Requirements • Installing • Usage • Documentation • Changelog • Communication • Contributing • Author • License
Queuer is a queue manager, built on top of OperationQueue and Dispatch (aka GCD).
It allows you to create any synchronous and asynchronous task easily, with just a few lines.
Here is the list of all the features:
- Works on all Swift compatible platforms (even Linux
*
) - Easy to use
- Well documented (100% documented)
- Well tested (currently 99% code coverage)
- Create an operation block
- Create a single operation
- Create chained operations
- Manage a centralized queue
- Create unlimited queue
- Declare how many concurrent operation a queue can handle
- Create a network request operation
*
- Create a network download operation
*
- Create a network upload operation
*
- Ability to restore uncompleted network operations
*
*
Currently,URLSession.shared
property is not yet implemented on Linux, alsoQualityOfService
property is not directly supported on Linux, since there are not qos class promotions available outside of darwin targets.
Swift | Xcode | Queuer | iOS | macOS | tvOS | watchOS | Linux |
---|---|---|---|---|---|---|---|
3.1...3.2 | 8.3...9.0 | 1.0.0...1.1.0 | 8.0+ | 10.10 | 9.0 | 2.0+ | * |
4.0 | 9.0 | 1.2.0 | 8.0+ | 10.10 | 9.0 | 2.0+ | * |
*
Currently,URLSession.shared
property is not yet implemented on Linux, alsoQualityOfService
property is not directly supported on Linux, since there are not qos class promotions available outside of darwin targets.
See Requirements section to check Swift, Xcode, Queuer and OS versions.
- Open and build the framework from the project (Queuer.xcodeproj)
- Import Queuer.framework into your project
- Import the framework with
import Queuer
- Enjoy!
-
Create a Podfile in your project directory and write into:
platform :ios, '8.0' xcodeproj 'Project.xcodeproj' use_frameworks! pod 'Queuer'
-
Change "Project" with your real project name
-
Open Terminal, go to your project directory and type:
pod install
-
Import the framework with
import Queuer
-
Enjoy!
-
Create a Cartfile in your project directory and write into:
github "FabrizioBrancati/Queuer"
-
Open Terminal, go to project directory and type:
carthage update
-
Include the created Framework in your project
-
Add Build Phase with the following contents:
/usr/local/bin/carthage copy-frameworks
and add the paths to the Queuer framework under Input Files
$(SRCROOT)/Carthage/Build/iOS/Queuer.framework
This script works around an App Store submission bug triggered by universal binaries and ensures that necessary bitcode-related files are copied when archiving
-
Import the framework with
import Queuer
-
Enjoy!
-
Create a Package.swift file in your project directory and write into:
import PackageDescription let package = Package( name: "Project", products: [ .executable(name: "Project", targets: ["Project"]) ], dependencies: [ .package(url: "https://github.com/FabrizioBrancati/Queuer.git", .upToNextMajor(from: "1.0.0")) ], targets: [ .target(name: "Project", dependencies: ["BFKit"]) ] )
-
Change "Project" with your real project name
-
Open Terminal, go to project directory and type:
swift build
-
Import the framework with
import Queuer
-
Enjoy!
Queuer.shared.addOperation(operation)
let queue = Queuer(name: "MyCustomQueue")
You can even create a queue by defining the maxConcurrentOperationCount
and the qualityOfService
*
properties:
let queue = Queuer(name: "MyCustomQueue", maxConcurrentOperationCount: Int.max, qualityOfService: .default)
*
Currently,QualityOfService
property is not directly supported on Linux, since there are not qos class promotions available outside of darwin targets.
You have three methods to add an Operation
block:
-
Directly on the
queue
(orQueuer.shared
):queue.addOperation { /// Your task here }
-
Creating a
ConcurrentOperation
with a block:let concurrentOperation = ConcurrentOperation { /// Your task here } queue.addOperation(concurrentOperation)
-
Creating a
SynchronousOperation
with a block:let synchronousOperation = SynchronousOperation { /// Your task here } queue.addOperation(concurrentOperation)
We will see how
ConcurrentOperation
andSynchronousOperation
works later.
Chained Operations are operations that add a dependency each other.
They follow the given array order, for example: [A, B, C] = A -> B -> C -> completionBlock
.
let concurrentOperation1 = ConcurrentOperation {
/// Your task 1 here
}
let concurrentOperation2 = ConcurrentOperation {
/// Your task 2 here
}
queue.addChainedOperations([concurrentOperation1, concurrentOperation2]) {
/// Your completion task here
}
-
Cancel all operations in queue:
queue.cancelAll()
-
Pause queue:
queue.pause()
By calling
pause()
you will not be sure that every operation will be paused. If the Operation is already started it will not be on pause until it's a custom Operation that overridespause()
function or is aRequestOperation
. -
Resume queue:
queue.resume()
To have a complete
pause
andresume
states you must create a custom Operation that overridespause()
andresume()
function or use aRequestOperation
. -
Wait until all operations are finished:
queue.waitUntilAllOperationsAreFinished()
This function means that the queue will blocks the current thread until all operations are finished.
ConcurrentOperation
is a class created to be subclassed.
It allows synchronous and asynchronous tasks, has a pause and resume states, can be easily added to a queue and can be created with a block.
You can create your custom ConcurrentOperation
by subclassing it.
You must override execute()
function and call the finish()
function inside it, when the task has finished its job to notify the queue.
Look at RequestOperation.swift if you are looking for an example.
For convenience it has an init
function with a completion block:
let concurrentOperation = ConcurrentOperation {
/// Your task here
}
concurrentOperation.addToQueue(queue)
There are three methods to create synchronous tasks or even queue:
- Setting
maxConcurrentOperationCount
of the queue to1
.
By setting that property to1
you will be sure that only one task at time will be executed. - Using a
Semaphore
and waiting until a task has finished its job. - Using a
SynchronousOperation
.
It's a subclass ofConcurrentOperation
that handles synchronous tasks.
It's not awesome as it seems to be and is always better to create an asynchronous task, but some times it may be useful.
For convenience it has an init
function with a completion block:
let synchronousOperation = SynchronousOperation {
/// Your task here
}
synchronousOperation.addToQueue(queue)
A Semaphore
is a struct that uses the GDC's DispatchSemaphore
to create a semaphore on the function and wait until it finish its job.
I recommend you to use a defer { semaphore.continue() }
right after the Semaphore
creation and wait()
call.
let semaphore = Semaphore()
semaphore.wait()
defer { semaphore.continue() }
/// Your task here
It's more useful if used inside an asynchronous task:
let concurrentOperation = ConcurrentOperation {
/// Your task here
semaphore.continue()
}
concurrentOperation.addToQueue(queue)
semaphore.wait()
RequestOperation
allows you to easily create a network request and add it to a queue:
let requestOperation: RequestOperation = RequestOperation(url: self.testAddress) { success, response, data, error in
}
requestOperation.addToQueue(queue)
Allowed parameters in RequestOperation
init
function:
url
is aString
representing the request URLquery
isDictionary
representing the request query parameters to be added to theurl
with?
and&
characterstimeout
is the request timeoutmethod
is the request method, you can choose to one of:connect
,delete
,get
,head
,options
,patch
,post
andput
cachePolicy
is the request cache policy, referrer to CachePolicy documentationheaders
is aDictionary
representing the request headersbody
is aData
representing the request bodycompletionHandler
is the request response handler
Response handler variables:
success
is aBool
indicating if the request was successful. It's successful if its status is between 200 and 399, it wasn't cancelled and did't get any other network error.respose
is anHTTPURLResponse
instance. It contains all the response headers and the status code. May benil
.data
is aData
instance with the request body. You must convert, to a JSON or String in example, it in order to use. May benil
.error
is anError
instance with the request error. May benil
.
It can be pause
d, resume
d, cancel
led and chained with other Operation
s.
*
Currently,URLSession.shared
property is not yet implemented on Linux.
100% Documented
To see what has changed in recent versions of Queuer, see the CHANGELOG.md file.
- If you need help, open an issue.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, see Contributing section.
See CONTRIBUTING.md file.
Fabrizio Brancati
Website: https://www.fabriziobrancati.com
Email: [email protected]
Queuer is available under the MIT license. See the LICENSE file for more info.