AmplifyOperation

open class AmplifyOperation<Request, Success, Failure> : AsynchronousOperation where Request : AmplifyOperationRequest, Failure : AmplifyError
extension AmplifyOperation: CategoryTypeable
extension AmplifyOperation: HubPayloadEventNameable
extension AmplifyOperation: Cancellable

An abstract representation of an Amplify unit of work. Subclasses may aggregate multiple work items to fulfull a single “AmplifyOperation”, such as an “extract text operation” which might include uploading an image to cloud storage, processing it via a Predictions engine, and translating the results.

AmplifyOperations are used by plugin developers to perform tasks on behalf of the calling app. They have a default implementation of a dispatch method that sends a contextualized payload to the Hub.

Pausable/resumable tasks that do not require Hub dispatching should use AsynchronousOperation instead.

  • The concrete Request associated with this operation

    Declaration

    Swift

    public typealias Request = Request
  • The concrete Success type associated with this operation

    Declaration

    Swift

    public typealias Success = Success
  • The concrete Failure type associated with this operation

    Declaration

    Swift

    public typealias Failure = Failure
  • Convenience typealias defining the Results dispatched by this operation

    Declaration

    Swift

    public typealias OperationResult = Result<Success, Failure>
  • Convenience typealias for the listener callback submitted during Operation creation

    Declaration

    Swift

    public typealias ResultListener = (OperationResult) -> Void
  • id

    The unique ID of the operation. In categories where operations are persisted for future processing, this id can be used to identify previously-scheduled work for progress tracking or other functions.

    Declaration

    Swift

    public let id: UUID
  • Incoming parameters of the original request

    Declaration

    Swift

    public let request: Request
  • All AmplifyOperations must be associated with an Amplify Category

    Declaration

    Swift

    public let categoryType: CategoryType
  • All AmplifyOperations must declare a HubPayloadEventName

    Declaration

    Swift

    public let eventName: HubPayloadEventName
  • Creates an AmplifyOperation for the specified reequest.

    Events

    An AmplifyOperation will dispatch messages to the Hub as it completes its work. The HubPayload for these messages will have the following structure:

    • eventName: The event name defined by the operation , such as “Storage.getURL” or “Storage.downloadFile”. See HubPayload.EventName for a list of pre-defined event names.
    • context: An AmplifyOperationContext whose operationId will be the ID of this operation, and whose request will be the Request used to create the operation.
    • data: The OperationResult that will be dispatched to an event listener. Event types for the listener are derived from the request.

    A caller may specify a listener during a call to an Amplify category API:

    Amplify.Storage.list { event in print(event) }
    

    Or after the fact, by passing the operation to the Hub:

    Amplify.Hub.listen(to: operation) { event in print(event) }
    

    In either of these cases, Amplify creates a HubListener for the operation by:

    1. Filtering messages by the operation’s ID
    2. Extracting the HubPayload’s data element and casts it to the expected OperationResult type for the listener
    3. Automatically unsubscribing the listener (by calling Amplify.Hub.removeListener) when the listener receives a result

    Callers can remove the listener at any time by calling operation.removeResultListener().

    Declaration

    Swift

    public init(categoryType: CategoryType,
                eventName: HubPayloadEventName,
                request: Request,
                resultListener: ResultListener? = nil)

    Parameters

    categoryType

    The categoryType of this operation

    eventName

    The event name of this operation, used in HubPayload messages dispatched by the operation

    request

    The request used to generate this operation

    resultListener

    The optional listener for the OperationResults associated with the operation

  • Classes that override this method must emit a completion to the resultPublisher upon cancellation

    Declaration

    Swift

    open override func cancel()
  • Dispatches an event to the hub. Internally, creates an AmplifyOperationContext object from the operation’s id, and request. On iOS 13+, this method also publishes the result on the resultPublisher.

    Declaration

    Swift

    public func dispatch(result: OperationResult)

    Parameters

    result

    The OperationResult to dispatch to the hub as part of the HubPayload

  • Removes the listener that was registered during operation instantiation

    Declaration

    Swift

    public func removeResultListener()
  • Publishes the final result of the operation

    Declaration

    Swift

    var resultPublisher: AnyPublisher<Success, Failure> { get }