修复因为创建datashare导致的CPU占用高的问题

Signed-off-by: zhengyongjie <15531316327@163.com>
This commit is contained in:
zhengyongjie 2024-06-20 15:22:27 +08:00
parent 329a825fc9
commit 78785ecb8e
2 changed files with 28 additions and 16 deletions

View File

@ -28,6 +28,8 @@ const TAG = 'SettingsDataManager'
class SettingsDataManager {
private readonly uriShare: string = 'datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=';
private dataShareHelper: dataShare.DataShareHelper | null = null;
private RETRY_INTERVAL_MS = 2000;
private MAX_RETRY_TIME = 10;
private constructor() {
}
@ -43,21 +45,30 @@ class SettingsDataManager {
return globalThis.SettingsDataManagerInstance;
}
public createDataShareHelper() {
Log.showInfo(TAG, 'createDataShareHelper context:' + globalThis.desktopContext);
const UPDATE_INTERVAL = 30;
const timer = setInterval(() => {
dataShare.createDataShareHelper(globalThis.desktopContext, this.uriShare)
.then((dataHelper) => {
Log.showInfo(TAG, `createDataShareHelper success.`);
this.dataShareHelper = dataHelper;
globalThis.sGestureNavigationManager.getGestureNavigationStatus();
clearInterval(timer);
})
.catch((err: BusinessError) => {
Log.showError(TAG, `createDataShareHelper fail. ${JSON.stringify(err)}`);
});
}, UPDATE_INTERVAL);
private sleep (time: number) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
public async createDataShareHelper(retryTimes: number): Promise<void> {
Log.showError(TAG, 'createDataShareHelper');
if (retryTimes < 1) {
Log.showError(TAG, 'createDataShareHelper, retry too many times');
return;
}
Log.showError(TAG, 'createDataShareHelper in, retry times: %{public}d', this.MAX_RETRY_TIME - retryTimes + 1);
try {
this.dataShareHelper = await dataShare.createDataShareHelper(globalThis.desktopContext, this.uriShare);
if (this.dataShareHelper) {
Log.showInfo(TAG, 'createDataShareHelper success.');
globalThis.sGestureNavigationManager.getGestureNavigationStatus();
}
} catch (err) {
Log.showError(TAG, 'createDataShareHelper error, code: ' + err?.code + ', message: ' + err?.message);
await this.sleep(this.RETRY_INTERVAL_MS);
this.createDataShareHelper(retryTimes - 1);
}
}
/**

View File

@ -31,6 +31,7 @@ const TAG = 'GestureNavigationManage';
export class GestureNavigationManager {
private readonly uri: string | null = null;
private helper: dataShare.DataShareHelper;
private MAX_RETRY_TIME = 10;
private readonly sGestureNavigationExecutors: GestureNavigationExecutors = GestureNavigationExecutors.getInstance();
private touchEventCallback: inputMonitor.TouchEventReceiver | null = null;
@ -63,7 +64,7 @@ export class GestureNavigationManager {
globalThis.sGestureNavigationExecutors.setScreenHeight(display.height);
this.touchEventCallback = globalThis.sGestureNavigationExecutors.touchEventCallback
.bind(globalThis.sGestureNavigationExecutors);
settingsDataManager.createDataShareHelper();
settingsDataManager.createDataShareHelper(this.MAX_RETRY_TIME);
}
}