From 556ee5575ee7039ac25533c842b28968e441a027 Mon Sep 17 00:00:00 2001 From: zwx1126739 Date: Thu, 1 Aug 2024 12:00:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dkvstore=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwx1126739 --- .../entry/src/main/ets/model/KvStoreModel.ets | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/code/SuperFeature/DistributedAppDev/Kvstore/entry/src/main/ets/model/KvStoreModel.ets b/code/SuperFeature/DistributedAppDev/Kvstore/entry/src/main/ets/model/KvStoreModel.ets index d402fdb3d..d49cc600b 100644 --- a/code/SuperFeature/DistributedAppDev/Kvstore/entry/src/main/ets/model/KvStoreModel.ets +++ b/code/SuperFeature/DistributedAppDev/Kvstore/entry/src/main/ets/model/KvStoreModel.ets @@ -13,13 +13,17 @@ * limitations under the License. */ -import distributedData from '@ohos.data.distributedKVStore' -import common from '@ohos.app.ability.common' -import Logger from '../util/Logger' +import distributedData from '@ohos.data.distributedKVStore'; +import common from '@ohos.app.ability.common'; +import Logger from '../util/Logger'; import { Callback } from '@ohos.base'; +import distributedDeviceManager from '@ohos.distributedDeviceManager'; -const STORE_ID = 'etskvstore' -const TAG = 'KvStoreModel' +const STORE_ID = 'etskvstore'; +const TAG = 'KvStoreModel'; +const mode = distributedData.SyncMode.PUSH_PULL; +let context: common.Context = getContext(this); +let devManager: distributedDeviceManager.DeviceManager; export class KvStoreModel { private kvManager?: distributedData.KVManager = undefined; @@ -49,7 +53,7 @@ export class KvStoreModel { createIfMissing: true, encrypt: false, backup: false, - autoSync: true, + autoSync: false, kvStoreType: distributedData.KVStoreType.SINGLE_VERSION, securityLevel: distributedData.SecurityLevel.S1, } @@ -72,6 +76,21 @@ export class KvStoreModel { value ).then((data) => { Logger.debug(TAG, `kvStore.put ${key} finished, data= ${JSON.stringify(data)}`); + try { + devManager = distributedDeviceManager.createDeviceManager(context.applicationInfo.name); + let deviceIds: string[] = []; + if (devManager != null) { + let devices = devManager.getAvailableDeviceListSync(); + for (let i = 0; i < devices.length; i++) { + deviceIds[i] = devices[i].networkId as string; + } + } + if (this.kvStore != null) { + this.kvStore.sync(deviceIds, mode, 1000); + } + } catch (err) { + Logger.error('createDeviceManager errCode:' + err.code + ',errMessage:' + err.message); + } }).catch((err: object) => { Logger.debug(TAG, `kvStore.put ${key} failed, ${JSON.stringify(err)}`); })