fix createSubcriber error

Signed-off-by: JIAO <jiaoyang33@huawei.com>
This commit is contained in:
JIAO
2025-11-04 16:10:04 +08:00
parent 6934e605ac
commit dbb0d50889
3 changed files with 10 additions and 4 deletions
@@ -21,6 +21,7 @@ import ohos.labels.APILevel
import ohos.ffi.{Callback1Param, INVALID_CODE}
import ohos.common_event_data.CCommonEventData
import ohos.common_event_publish_data.CCommonEventPublishData
import ohos.common_event_subscribe_info.CSubscribeInfo
import ohos.business_exception.{BusinessException, AsyncCallback}
public import ohos.common_event_data.CommonEventData
public import ohos.common_event_publish_data.CommonEventPublishData
@@ -75,12 +76,15 @@ public class CommonEventManager {
syscap: "SystemCapability.Notification.CommonEvent"
]
public static func createSubscriber(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber {
let ret = unsafe { CJ_CreateSubscriber(0) }
if (ret == Int64(INVALID_CODE)) {
var errorCode: Int32 = 0
var info = CSubscribeInfo(subscribeInfo)
let id = unsafe { FfiCommonEventManagerCreateSubscriber(inout info, inout errorCode) }
info.free()
if (errorCode == INVALID_CODE) {
throw BusinessException(getErrorCode(ERROR_CES_UNINITIALIZED),
"CommonEventManager: createSubscriber failed: ${getErrorMsg(ERROR_CES_UNINITIALIZED)}")
}
return CommonEventSubscriber(ret)
return CommonEventSubscriber(id)
}
/**
@@ -34,5 +34,7 @@ foreign {
func CJ_Subscribe(id: Int64, funcId: Int64): Int32
func CJ_Unsubscribe(id: Int64): Int32
func FfiCommonEventManagerCreateSubscriber(info: CPointer<CSubscribeInfo>, errorCode: CPointer<Int32>): Int64
}
@@ -43,7 +43,7 @@ protected struct CSubscribeInfo {
publisherPermission = LibC.mallocCString(info.publisherPermission)
publisherDeviceId = LibC.mallocCString(info.publisherDeviceId)
publisherBundleName = LibC.mallocCString(info.publisherBundleName)
} catch(e: BusinessException) {
} catch(e: Exception) {
LibC.free(publisherPermission)
LibC.free(publisherDeviceId)
LibC.free(publisherBundleName)