中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
键值型数据库如何将本设备的数据同步到组网中的其他设备中去?
微信扫码分享
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) } }