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 true or false:

    • 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 your satisfyConstraint method.

    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) -> Bool

    Parameters

    resource

    NetworkResource

    Return Value

    Bool

  • This method is called asynchronously when the isConstraintSatisfied method 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.async if you want to do something on the main thread.

    When done, make sure the finish method is called to let Swifty continue the tasks that were waiting on this Constraint.

    Declaration

    Swift

    open func satisfyConstraint(for resource: NetworkResource)

    Parameters

    resource

    NetworkResource

  • Informs Swifty that the constraint has finished.

    • If the constraint finishes without error, then the tasks waiting on this Constraint begin executing (subject to satisfaction of the task’s other constraints).
    • If the constraint finishes with error, then the tasks waiting on this Constraint also fail with the this error.

    Declaration

    Swift

    public func finish(with error: Error?)