Skip to content

Commit 4974933

Browse files
Improve readme and add pause, resume, and cancel APIs
1 parent 2dde223 commit 4974933

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

README.md

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,31 @@ Add the dependency to any targets you've declared in your manifest:
5757

5858
## Usage
5959

60-
- [Shared Queuer](https://github.com/FabrizioBrancati/Queuer#shared-queuer)
61-
- [Custom Queue](https://github.com/FabrizioBrancati/Queuer#custom-queue)
60+
- [Using the Shared Queuer](https://github.com/FabrizioBrancati/Queuer#using-the-shared-queuer)
61+
- [Create a Custom Queue](https://github.com/FabrizioBrancati/Queuer#create-a-custom-queue)
6262
- [Create an Operation Block](https://github.com/FabrizioBrancati/Queuer#create-an-operation-block)
63-
- [Chained Operations](https://github.com/FabrizioBrancati/Queuer#chained-operations)
64-
- [Group Operations](https://github.com/FabrizioBrancati/Queuer#group-operations)
65-
- [Queue States](https://github.com/FabrizioBrancati/Queuer#queue-states)
66-
- [Synchronous Queue](https://github.com/FabrizioBrancati/Queuer#synchronous-queue)
63+
- [Use Chained Operations](https://github.com/FabrizioBrancati/Queuer#use-chained-operations)
64+
- [Use Group Operations](https://github.com/FabrizioBrancati/Queuer#use-group-operations)
65+
- [Available Queue States](https://github.com/FabrizioBrancati/Queuer#available-queue-states)
66+
- [Control Operation States](https://github.com/FabrizioBrancati/Queuer#control-operation-states)
67+
- [Use a Synchronous Queue](https://github.com/FabrizioBrancati/Queuer#use-a-synchronous-queue)
6768
- [Create a Custom Operation](https://github.com/FabrizioBrancati/Queuer#create-a-custom-operation)
6869
- [Automatically Retry an Operation](https://github.com/FabrizioBrancati/Queuer#automatically-retry-an-operation)
6970
- [Manually Retry an Operation](https://github.com/FabrizioBrancati/Queuer#manually-retry-an-operation)
7071
- [Manually Finish an Operation](https://github.com/FabrizioBrancati/Queuer#manually-finish-an-operation)
7172
- [Async Task in an Operation](https://github.com/FabrizioBrancati/Queuer#async-task-in-an-operation)
72-
- [Scheduler](https://github.com/FabrizioBrancati/Queuer#scheduler)
73-
- [Semaphore](https://github.com/FabrizioBrancati/Queuer#semaphore)
73+
- [Set Up a Scheduler](https://github.com/FabrizioBrancati/Queuer#set-up-a-scheduler)
74+
- [Use a Semaphore](https://github.com/FabrizioBrancati/Queuer#use-a-semaphore)
7475

75-
### Shared Queuer
76+
### Using the Shared Queuer
7677

7778
Queuer offers a shared instance that you can use to add operations to a centralized queue:
7879

7980
```swift
8081
Queuer.shared.addOperation(operation)
8182
```
8283

83-
### Custom Queue
84+
### Create a Custom Queue
8485

8586
You can also create a custom queue:
8687

@@ -118,7 +119,7 @@ You have three methods to add an `Operation` block.
118119
> [!NOTE]
119120
> We will see how `ConcurrentOperation` works later.
120121

121-
### Chained Operations
122+
### Use Chained Operations
122123

123124
Chained Operations are `Operation`s that add a dependency each other.
124125

@@ -144,7 +145,7 @@ queue.addCompletionHandler {
144145
}
145146
```
146147

147-
### Group Operations
148+
### Use Group Operations
148149

149150
Group Operations are `Operation`s that handles a group of `Operation`s with a completion handler.
150151

@@ -174,7 +175,7 @@ queue.addChainedOperations([groupOperationA, concurrentOperationC]) {
174175

175176
In this case the output will be the following one: `[[A & B -> completionHandler] -> C] -> completionHandler`.
176177

177-
### Queue States
178+
### Available Queue States
178179

179180
There are a few method to handle the queue states.
180181

@@ -211,7 +212,34 @@ There are a few method to handle the queue states.
211212
> [!IMPORTANT]
212213
> This function means that the queue will blocks the current thread until all `Operation`s are finished.
213214

214-
### Synchronous Queue
215+
### Control Operation States
216+
217+
You can control the `ConcurrentOperation` states by calling `pause()`, `resume()`, `cancel()`.
218+
219+
> [!NOTE]
220+
> If you use a `Queuer` object with all `ConcurrentOperation` objects inside to manage your queue, you should avoid calling them directly on the `ConcurrentOperation` object, but you should call them on the `Queuer` one. The `Queuer` object will handle the `ConcurrentOperation` states automatically.
221+
222+
1. Pause a `ConcurrentOperation`:
223+
224+
```swift
225+
concurrentOperation.pause()
226+
```
227+
228+
2. Resume a `ConcurrentOperation`:
229+
230+
```swift
231+
concurrentOperation.resume()
232+
```
233+
234+
3. Cancel a `ConcurrentOperation`:
235+
236+
```swift
237+
concurrentOperation.cancel()
238+
```
239+
240+
For convenience, you can set closures to the `ConcurrentOperation` object to handle the states with `onPause`, `onResume`, and `onCancel` properties. They will be called when the `ConcurrentOperation` is paused, resumed, or canceled.
241+
242+
### Use a Synchronous Queue
215243

216244
Setting the `maxConcurrentOperationCount` property of a queue to `1` will make you sure that only one task at a time will be executed.
217245

@@ -315,7 +343,7 @@ concurrentOperation.manualFinish = true
315343
> [!CAUTION]
316344
> If you don't set `manualFinish` to `true`, your `Operation` will finish before the async task is completed.
317345
318-
### Scheduler
346+
### Set Up a Scheduler
319347

320348
A `Scheduler` is a struct that uses the GDC's `DispatchSourceTimer` to create a timer that can execute functions with a specified interval and quality of service.
321349

@@ -340,7 +368,7 @@ With `timer` property you can access to all `DispatchSourceTimer` properties and
340368
schedule.timer.cancel()
341369
```
342370

343-
### Semaphore
371+
### Use a Semaphore
344372

345373
A `Semaphore` is a struct that uses the GCD's `DispatchSemaphore` to create a semaphore on the function and wait until it finish its job.
346374

0 commit comments

Comments
 (0)