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

Available where Request == RESTOperation.Request, Success == RESTOperation.Success, Failure == RESTOperation.Failure

Available where Request == AuthAttributeResendConfirmationCodeOperation.Request, Success == AuthAttributeResendConfirmationCodeOperation.Success, Failure == AuthAttributeResendConfirmationCodeOperation.Failure

Available where Request == AuthChangePasswordOperation.Request, Success == AuthChangePasswordOperation.Success, Failure == AuthChangePasswordOperation.Failure

Available where Request == AuthConfirmResetPasswordOperation.Request, Success == AuthConfirmResetPasswordOperation.Success, Failure == AuthConfirmResetPasswordOperation.Failure

Available where Request == AuthConfirmSignInOperation.Request, Success == AuthConfirmSignInOperation.Success, Failure == AuthConfirmSignInOperation.Failure

Available where Request == AuthConfirmSignUpOperation.Request, Success == AuthConfirmSignUpOperation.Success, Failure == AuthConfirmSignUpOperation.Failure

Available where Request == AuthConfirmUserAttributeOperation.Request, Success == AuthConfirmUserAttributeOperation.Success, Failure == AuthConfirmUserAttributeOperation.Failure

Available where Request == AuthFetchDevicesOperation.Request, Success == AuthFetchDevicesOperation.Success, Failure == AuthFetchDevicesOperation.Failure

Available where Request == AuthFetchSessionOperation.Request, Success == AuthFetchSessionOperation.Success, Failure == AuthFetchSessionOperation.Failure

Available where Request == AuthFetchUserAttributeOperation.Request, Success == AuthFetchUserAttributeOperation.Success, Failure == AuthFetchUserAttributeOperation.Failure

Available where Request == AuthForgetDeviceOperation.Request, Success == AuthForgetDeviceOperation.Success, Failure == AuthForgetDeviceOperation.Failure

Available where Request == AuthRememberDeviceOperation.Request, Success == AuthRememberDeviceOperation.Success, Failure == AuthRememberDeviceOperation.Failure

Available where Request == AuthResendSignUpCodeOperation.Request, Success == AuthResendSignUpCodeOperation.Success, Failure == AuthResendSignUpCodeOperation.Failure

Available where Request == AuthResetPasswordOperation.Request, Success == AuthResetPasswordOperation.Success, Failure == AuthResetPasswordOperation.Failure

Available where Request == AuthSignInOperation.Request, Success == AuthSignInOperation.Success, Failure == AuthSignInOperation.Failure

Available where Request == AuthSignOutOperation.Request, Success == AuthSignOutOperation.Success, Failure == AuthSignOutOperation.Failure

Available where Request == AuthDeleteUserOperation.Request, Success == AuthDeleteUserOperation.Success, Failure == AuthDeleteUserOperation.Failure

Available where Request == AuthSignUpOperation.Request, Success == AuthSignUpOperation.Success, Failure == AuthSignUpOperation.Failure

Available where Request == AuthSocialWebUISignInOperation.Request, Success == AuthSocialWebUISignInOperation.Success, Failure == AuthSocialWebUISignInOperation.Failure

Available where Request == AuthUpdateUserAttributeOperation.Request, Success == AuthUpdateUserAttributeOperation.Success, Failure == AuthUpdateUserAttributeOperation.Failure

Available where Request == AuthUpdateUserAttributesOperation.Request, Success == AuthUpdateUserAttributesOperation.Success, Failure == AuthUpdateUserAttributesOperation.Failure

Available where Request == PredictionsIdentifyOperation.Request, Success == PredictionsIdentifyOperation.Success, Failure == PredictionsIdentifyOperation.Failure

Available where Request == PredictionsInterpretOperation.Request, Success == PredictionsInterpretOperation.Success, Failure == PredictionsInterpretOperation.Failure

Available where Request == PredictionsSpeechToTextOperation.Request, Success == PredictionsSpeechToTextOperation.Success, Failure == PredictionsSpeechToTextOperation.Failure

Available where Request == PredictionsTextToSpeechOperation.Request, Success == PredictionsTextToSpeechOperation.Success, Failure == PredictionsTextToSpeechOperation.Failure

Available where Request == PredictionsTranslateTextOperation.Request, Success == PredictionsTranslateTextOperation.Success, Failure == PredictionsTranslateTextOperation.Failure

Available where Request == StorageGetURLOperation.Request, Success == StorageGetURLOperation.Success, Failure == StorageGetURLOperation.Failure

Available where Request == StorageListOperation.Request, Success == StorageListOperation.Success, Failure == StorageListOperation.Failure

Available where Request == StorageRemoveOperation.Request, Success == StorageRemoveOperation.Success, Failure == StorageRemoveOperation.Failure