未验证 提交 75ad3c14 编写于 作者: W Wangkai 提交者: Gitee

update zh-cn/application-dev/database/database-mdds-guidelines.md.

Signed-off-by: NWangkai <wangkai424@huawei.com>
上级 91f7b492
...@@ -52,59 +52,40 @@ ...@@ -52,59 +52,40 @@
```js ```js
// FA模型 // 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(); grantPermission();
// Stage模型 // Stage模型
import AbilityStage from '@ohos.application.Ability'; import Ability from '@ohos.application.Ability';
import accessControl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle'; let context = null;
let appId = "appId"; function grantPermission() {
async function requestPermissions () { class MainAbility extends Ability {
let permissions: Array<string> = [ onWindowStageCreate(windowStage) {
"ohos.permission.DISTRIBUTED_DATASYNC" let context = this.context;
];
let needGrantPermission = false
let accessManger = accessControl.createAtManager()
console.info("[JSDemo]: app permission get bundle info")
let bundleInfo = bundle.getApplicationInfo(appId, 0, 100)
console.info(`[JSDemo]: app permission query permission ${bundleInfo.accessTokenId.toString()}`)
for (const permission of permissions) {
console.info(`[JSDemo]: app permission query grant status ${permission}`)
try {
let grantStatus = accessManger.verifyAccessToken(bundleInfo.accessTokenId, permission)
if (grantStatus === accessControl.GrantStatus.PERMISSION_DENIED) {
needGrantPermission = true
break;
}
} catch (err) {
console.error(`[JSDemo]: app permission query grant status error ${permission} ${JSON.stringify(err)}`)
needGrantPermission = true
break;
}
}
if (needGrantPermission) {
console.info("[JSDemo]: app permission needGrantPermission")
try {
globalThis.abilityContext.requestPermissionsFromUser(permissions)
} catch (err) {
console.error(`[JSDemo]: app permission ${JSON.stringify(err)}`)
} }
} else {
console.info("[JSDemo]: app permission already granted")
}
} }
let permissions = ['ohos.permission.DISTRIBUTED_DATASYNC'];
context.requestPermissionsFromUser(permissions).then((data) => {
console.log('success: ${data}');
}).catch((error) => {
console.log('failed: ${error}');
});
}
grantPermission();
``` ```
3. 根据配置构造分布式数据库管理类实例。 3. 根据配置构造分布式数据库管理类实例。
...@@ -120,7 +101,7 @@ ...@@ -120,7 +101,7 @@
let context = featureAbility.getContext(); let context = featureAbility.getContext();
// Stage模型获取context // Stage模型获取context
import AbilityStage from '@ohos.application.Ability' import AbilityStage from '@ohos.application.Ability';
let context = null; let context = null;
class MainAbility extends AbilityStage{ class MainAbility extends AbilityStage{
onWindowStageCreate(windowStage){ onWindowStageCreate(windowStage){
...@@ -140,14 +121,14 @@ ...@@ -140,14 +121,14 @@
} }
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}');
} }
``` ```
...@@ -171,14 +152,14 @@ ...@@ -171,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}');
} }
``` ```
...@@ -192,7 +173,7 @@ ...@@ -192,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}");
}); });
``` ```
...@@ -209,13 +190,13 @@ ...@@ -209,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}');
} }
``` ```
...@@ -232,16 +213,16 @@ ...@@ -232,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}');
} }
``` ```
...@@ -260,7 +241,7 @@ ...@@ -260,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方法得到
...@@ -275,7 +256,7 @@ ...@@ -275,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}');
} }
} }
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册