MoEngageModule
public extension MoEngageModule
-
Asynchronous lifecycle events that may suspend and must be awaited by the SDK.
Use
See moreAsyncEventfor operations that must complete before the SDK proceeds, such as flushing data before logout or checking unregister eligibility. Events are delivered viaItem.process(event:sdkInstance:) async, which runs on theMoEngageSDKInstanceactor’s executor and is awaited before the SDK continues.Declaration
Swift
enum AsyncEvent : Hashable, Sendable -
Synchronous lifecycle events that modules can respond to.
These events represent key points in a module’s lifecycle or significant SDK state transitions. Events are delivered via
Item.process(event:sdkInstance:), which runs on theMoEngageSDKInstanceactor’s executor.Core Lifecycle Order
For a typical app session:
.init— called once when module is registered.start— called after all modules are initialized.deinit— called when SDK instance is being removed (rare)
Declaration
Swift
enum Event : Hashable, @unchecked Sendable -
Metadata about a module including its name, version, and additional information.
This type provides structured information about SDK modules for:
- Version tracking: Which module versions are integrated
- Debugging: Identifying active modules in logs
- Analytics: Reporting module usage to the backend
- Configuration: Storing module-specific metadata
Payload Generation
When multiple modules are collected, they can be converted to a network payload format for reporting to the MoEngage backend.
Example
See morelet info = MoEngageModule.Info( name: "cards", version: "2.1.0", metadata: ["cardTypes": ["carousel", "stack"]] )Declaration
Swift
struct Info -
Protocol that all SDK modules must conform to.
Implementing this protocol allows a module to be registered with the SDK and participate in the module lifecycle. All methods are
staticand receive anisolated MoEngageSDKInstance, so they execute on the instance’s actor without an extra hop.Implementation Guidelines
State Management
- Store per-instance state keyed by the SDK instance’s workspace ID
- Clean up state in the
Event.deinitcase
Event Handling
- Return quickly from
process(event:sdkInstance:); spawn an unstructuredTask { }for expensive or async work you do not need to await - Use
process(event:sdkInstance:) asynconly for operations the SDK must await before proceeding (e.g. flushing data before logout)
Error Handling
- Don’t throw errors from these methods
- Log errors internally and handle gracefully
Example Implementation
See morefinal class AnalyticsModule: MoEngageModule.Item { static func getInfo(sdkInstance: isolated MoEngageSDKInstance) -> MoEngageModule.Info? { return .init( name: "analytics", version: "3.0.0", metadata: ["features": ["events", "userAttributes"]] ) } static func process(event: MoEngageModule.Event, sdkInstance: isolated MoEngageSDKInstance) { switch event { case .init: setupObservers(for: sdkInstance) case .start: Task { await trackAppOpened(for: sdkInstance) } case .deinit: removeState(for: sdkInstance) default: break } } static func process(event: MoEngageModule.AsyncEvent, sdkInstance: isolated MoEngageSDKInstance) async { switch event { case .willLogout: await flushPendingEvents(for: sdkInstance) default: break } } static func listensToAdditionalNotifications() -> [Notification.Name] { [] } }Declaration
Swift
protocol Item : Sendable, ItemReflection -
Marker protocol that exposes SDK modules to the Objective-C runtime.
Iteminherits from this protocol so the SDK can useNSClassFromStringand related reflection APIs to discover and instantiate modules registered by name — without requiring a direct Swift type reference at the call site.Important
Do not conform to
ItemReflectiondirectly. Conform toIteminstead.Declaration
Swift
@objc protocol ItemReflection