After a service is finished in the dynamic mode, the subscriber should proactively unsubscribe from the event. You can call unsubscribe() to unsubscribe from a common event that is no longer required.
| API | Description |
|---|---|
| unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback<void>) | Unsubscribes from a common event. |
Import the related modules.
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG: string = 'ProcessModel';
const DOMAIN_NUMBER: number = 0xFF00; Subscribe to an event by following the procedure described in Subscribing to Common Events in Dynamic Mode.
Call unsubscribe() in CommonEvent to unsubscribe from an event.
// The subscriber object is created during event subscription.
if (subscriberCustom !== null) {
commonEventManager.unsubscribe(subscriberCustom, (err: BusinessError) => {
if (err) {
hilog.error(DOMAIN_NUMBER, TAG,
`Failed to unsubscribe. code is ${err.code}, message is ${err.message}`);
} else {
hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in unsubscribing.`);
subscriberCustom = null;
}
})
}