This package contains three libraries:
Coordinate a sequence of modal user interactions.
Apps may need to present several modal interactions to the user, especially at startup.
For example:
It can be challenging to coordinate many such interactions:
Use the InteractionQueue to coordinate your interactions.
Each interaction presents its user interface and waits for the user’s response. The queue ensures that the next interaction doesn’t present its UI until the previous interaction has finished.
InteractionQueue is agnostic about which asynchronous programming model any particular interaction is using:
InteractionQueue is also thread-safe. Although it always starts interactions on the main (UI) queue, it can mark the running interaction as finished from any queue or thread.
InteractionQueue
in your main view controller.import InteractionQueue
⋮
private let interactionQueue = InteractionQueue()
The queue suspends itself when your view isn’t on screen.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
interactionQueue.onViewDidAppear()
}
override func viewWillDisappear(_ animated: Bool) {
interactionQueue.onViewWillDisappear()
super.viewWillDisappear(animated)
}
The interaction queue adds one operation at a time to the main (UI) queue and blocks until it’s finished.
interactionQueue.add { markInteractionFinished in
doSomethingWithEscapingCompletionBlock {
[weak self] in
defer { markOperationAsFinished() }
guard let self = self else { return }
// Rest of the completion block...
}
}
The queue passes a callback function (markInteractionFinished
) into the operation closure. You must call this function when you are finished with the interaction, even if the interaction gets cancelled or isn’t needed.
This is the only way to unblock the queue and proceed with the next interaction.
Hint: Put the call into a defer
block, as in the example above, before testing weak self
for nil
.
Even if self
has been dealloced/deinited, you will leak the queue and operation objects if you don’t mark the operation as finished, because the OS itself still retains strong references to them.
There are additional advantages to requiring an explicit mark-as-finished:
markInteractionFinished()
immediately if it doesn’t need to interact with the user.An asynchronous Operation
for use with an OperationQueue
.
Typical scenarios:
@escaping
completion block (closure) as a parameter.
OperationQueue
needs an accurate indication of completion in order to schedule the dependent tasks.Task
that uses await
to call an async
function.
Task
completes.link |
Stars: 1 |
Last commit: 3 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics