#鸿蒙学习大百科#键值型数据库如何将本设备的数据同步到组网中的其他设备中去?

键值型数据库如何将本设备的数据同步到组网中的其他设备中去?

HarmonyOS
2024-10-24 09:03:54
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
小猫的老公
import deviceManager from '@ohos.distributedDeviceManager';
let devManager: deviceManager.DeviceManager;
@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("").onClick(() => {
        try {
          // create deviceManager
          devManager = deviceManager.createDeviceManager(getContext(this).applicationInfo.name);
          // deviceIds由deviceManager调用getAvailableDeviceListSync方法得到
          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;
            }
          }
          try {
            // 1000表示最大延迟时间为1000ms   PUSH_ONLY:将本设备数据同步到其他设备
            kvStore.sync(deviceIds, distributedKVStore.SyncMode.PUSH_ONLY, 1000);
          } catch (e) {
            let error = e as BusinessError;
            console.error(`An unexpected error occurred. Code:${error.code},message:${error.message}`);
          }

        } catch (err) {
          let error = err as BusinessError;
          console.error("createDeviceManager errCode:" + error.code + ",errMessage:" + error.message);
        }
      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-10-24 18:09:32
相关问题
键值数据库设备数据同步demo
853浏览 • 1回复 待解决