Constraint
open class Constraint
Constraint: Constraints are tasks which can hold network requests from starting until they are satisfied.
-
Public initializer
Declaration
Swift
public init() -
This method is called on your constraint for every resource (with
canHaveConstraints = true) that passes through Swifty.This is where you decide if you need to perform some action for the given resource to satisfy your constraint. Based on the decision, this method needs to return
trueorfalse:- If you return
true, then the given resource will not wait for this Constraint. - In you return
false, then the given resource will start waiting for this Constraint, and Swifty will asynchronously call yoursatisfyConstraintmethod.
This method is called synchronously on a background thread.
This method is thread-safe, so you don’t need to worry about multiple threads calling this method at the same time. Swifty internally locks access to this method to one resource at a time.
Declaration
Swift
open func isConstraintSatisfied(for resource: NetworkResource) -> BoolParameters
resourceNetworkResource
Return Value
Bool
- If you return
-
This method is called asynchronously when the
isConstraintSatisfiedmethod returns false.This is where the actual operation can be performed. The operation can be any task, not just a network request.
This method is always called on a background thread. Use
DispatchQueue.main.asyncif you want to do something on the main thread.When done, make sure the
finishmethod is called to let Swifty continue the tasks that were waiting on this Constraint.Declaration
Swift
open func satisfyConstraint(for resource: NetworkResource)Parameters
resourceNetworkResource
-
Informs Swifty that the constraint has finished.
- If the constraint finishes
withouterror, then the tasks waiting on this Constraint begin executing (subject to satisfaction of the task’s other constraints). If the constraint finishes
witherror, then the tasks waiting on this Constraint also fail with the this error.
Declaration
Swift
public func finish(with error: Error?) - If the constraint finishes
View on GitHub
Install in Dash
Constraint Class Reference