未验证 提交 1c860d94 编写于 作者: O openharmony_ci 提交者: Gitee

!9959 分布式数据管理文档完善

Merge pull request !9959 from Wangkai/master
...@@ -50,18 +50,41 @@ ...@@ -50,18 +50,41 @@
这个权限还需要在应用首次启动的时候弹窗获取用户授权,可以通过如下代码实现: 这个权限还需要在应用首次启动的时候弹窗获取用户授权,可以通过如下代码实现:
```js ```js
// FA模型
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
function grantPermission() { function grantPermission() {
console.info('grantPermission'); console.info('grantPermission');
let context = featureAbility.getContext(); let context = featureAbility.getContext();
context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) { context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666).then((data) => {
console.info(`result.requestCode=${result.requestCode}`) console.info('success: ${data}');
}).catch((error) => {
}) console.info('failed: ${error}');
console.info('end grantPermission'); })
} }
grantPermission();
// Stage模型
import Ability from '@ohos.application.Ability';
let context = null;
function grantPermission() {
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
}
}
let permissions = ['ohos.permission.DISTRIBUTED_DATASYNC'];
context.requestPermissionsFromUser(permissions).then((data) => {
console.log('success: ${data}');
}).catch((error) => {
console.log('failed: ${error}');
});
}
grantPermission(); grantPermission();
``` ```
...@@ -73,25 +96,39 @@ ...@@ -73,25 +96,39 @@
以下为创建分布式数据库管理器的代码示例: 以下为创建分布式数据库管理器的代码示例:
```js ```js
// FA模型获取context
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
// Stage模型获取context
import AbilityStage from '@ohos.application.Ability';
let context = null;
class MainAbility extends AbilityStage{
onWindowStageCreate(windowStage){
context = this.context;
}
}
let kvManager; let kvManager;
try { try {
const kvManagerConfig = { const kvManagerConfig = {
bundleName: 'com.example.datamanagertest', bundleName: 'com.example.datamanagertest',
userInfo: { userInfo: {
context:context,
userId: '0', userId: '0',
userType: distributedData.UserType.SAME_USER_ID userType: distributedData.UserType.SAME_USER_ID
} }
} }
distributedData.createKVManager(kvManagerConfig, function (err, manager) { distributedData.createKVManager(kvManagerConfig, function (err, manager) {
if (err) { if (err) {
console.log("createKVManager err: " + JSON.stringify(err)); console.log('createKVManager err: ${error}');
return; return;
} }
console.log("createKVManager success"); console.log('createKVManager success');
kvManager = manager; kvManager = manager;
}); });
} catch (e) { } catch (e) {
console.log("An unexpected error occurred. Error: " + e); console.log('An unexpected error occurred. Error: ${e}');
} }
``` ```
...@@ -115,14 +152,14 @@ ...@@ -115,14 +152,14 @@
}; };
kvManager.getKVStore('storeId', options, function (err, store) { kvManager.getKVStore('storeId', options, function (err, store) {
if (err) { if (err) {
console.log("getKVStore err: " + JSON.stringify(err)); console.log('getKVStore err: ${err}');
return; return;
} }
console.log("getKVStore success"); console.log('getKVStore success');
kvStore = store; kvStore = store;
}); });
} catch (e) { } catch (e) {
console.log("An unexpected error occurred. Error: " + e); console.log('An unexpected error occurred. Error: ${e}');
} }
``` ```
...@@ -136,7 +173,7 @@ ...@@ -136,7 +173,7 @@
```js ```js
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) { kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
console.log("dataChange callback call data: " + JSON.stringify(data)); console.log("dataChange callback call data: ${data}");
}); });
``` ```
...@@ -153,13 +190,13 @@ ...@@ -153,13 +190,13 @@
try { try {
kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err, data) { kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err, data) {
if (err != undefined) { if (err != undefined) {
console.log("put err: " + JSON.stringify(err)); console.log('put err: ${error}');
return; return;
} }
console.log("put success"); console.log('put success');
}); });
} catch (e) { } catch (e) {
console.log("An unexpected error occurred. Error: " + e); console.log('An unexpected error occurred. Error: ${e}');
} }
``` ```
...@@ -176,16 +213,16 @@ ...@@ -176,16 +213,16 @@
try { try {
kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err, data) { kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err, data) {
if (err != undefined) { if (err != undefined) {
console.log("put err: " + JSON.stringify(err)); console.log('put err: ${error}');
return; return;
} }
console.log("put success"); console.log('put success');
kvStore.get(KEY_TEST_STRING_ELEMENT, function (err, data) { kvStore.get(KEY_TEST_STRING_ELEMENT, function (err, data) {
console.log("get success data: " + data); console.log('get success data: ${data}');
}); });
}); });
} catch (e) { } catch (e) {
console.log("An unexpected error occurred. Error: " + e); console.log('An unexpected error occurred. Error: ${e}');
} }
``` ```
...@@ -204,7 +241,7 @@ ...@@ -204,7 +241,7 @@
let devManager; let devManager;
// create deviceManager // create deviceManager
deviceManager.createDeviceManager("bundleName", (err, value) => { deviceManager.createDeviceManager('bundleName', (err, value) => {
if (!err) { if (!err) {
devManager = value; devManager = value;
// deviceIds由deviceManager调用getTrustedDeviceListSync方法得到 // deviceIds由deviceManager调用getTrustedDeviceListSync方法得到
...@@ -219,7 +256,7 @@ ...@@ -219,7 +256,7 @@
// 1000表示最大延迟时间为1000ms // 1000表示最大延迟时间为1000ms
kvStore.sync(deviceIds, distributedData.SyncMode.PUSH_ONLY, 1000); kvStore.sync(deviceIds, distributedData.SyncMode.PUSH_ONLY, 1000);
} catch (e) { } catch (e) {
console.log("An unexpected error occurred. Error: " + e); console.log('An unexpected error occurred. Error: ${e}');
} }
} }
}); });
......
...@@ -1008,7 +1008,7 @@ try { ...@@ -1008,7 +1008,7 @@ try {
}).catch((err) => { }).catch((err) => {
console.log('getResultSet failed: ' + err); console.log('getResultSet failed: ' + err);
}); });
const moved5 = resultSet.move(); const moved5 = resultSet.move(1);
console.log("move succeed:" + moved5); console.log("move succeed:" + moved5);
} catch (e) { } catch (e) {
console.log("move failed: " + e); console.log("move failed: " + e);
...@@ -1048,7 +1048,7 @@ try { ...@@ -1048,7 +1048,7 @@ try {
}).catch((err) => { }).catch((err) => {
console.log('getResultSet failed: ' + err); console.log('getResultSet failed: ' + err);
}); });
const moved6 = resultSet.moveToPosition(); const moved6 = resultSet.moveToPosition(1);
console.log("moveToPosition succeed: " + moved6); console.log("moveToPosition succeed: " + moved6);
} catch (e) { } catch (e) {
console.log("moveToPosition failed: " + e); console.log("moveToPosition failed: " + e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册