提交 1226f3ce 编写于 作者: L liusaimei

modify datashare

Signed-off-by: Nliusaimei <lirui.wx1229126@huawei.com>
上级 2330474a
...@@ -199,7 +199,7 @@ try { ...@@ -199,7 +199,7 @@ try {
| 名称 | 类型 | 必填 | 说明 | | 名称 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | string | 是 | 指定发布数据的键。 | | key | string | 是 | 指定发布数据的键。 |
| data | string \| ArrayBuffer | 是 | 指定发布的数据。如果数据很大,请使用ArrayBuffer。 | | data | string \| ArrayBuffer | 是 | 指定发布的数据。如果数据很大,请使用ArrayBuffer,建议20KB及以上使用ArrayBuffer。 |
| subscriberId | string | 是 | 指定订阅者id。 | | subscriberId | string | 是 | 指定订阅者id。 |
## RdbDataChangeNode<sup>10+</sup> ## RdbDataChangeNode<sup>10+</sup>
......
...@@ -21,115 +21,19 @@ ...@@ -21,115 +21,19 @@
**适配指导** **适配指导**
示例代码如下: 示例代码如下:
变更前代码示例:
**示例**
```ts
import rpc from '@ohos.rpc';
function onPublishCallback(err, node:dataShare.PublishedDataChangeNode) {
console.info("onPublishCallback node bundleName " + JSON.stringify(node.bundleName));
console.info("onPublishCallback node data size" + node.data.length);
for (let i = 0; i < node.data.length; i++) {
console.info("onPublishCallback node " + typeof node.data[i].data);
if (typeof node.data[i].data != 'string') {
let ash:rpc.Ashmem = node.data[i].data;
ash.mapReadonlyAshmem();
console.info("onPublishCallback " + JSON.stringify(ash.readAshmem(ash.getAshmemSize()/4, 0)));
ash.closeAshmem();
}
console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i]));
}
}
let uris:Array<string> = ['city', 'datashareproxy://com.acts.ohos.data.datasharetest/appInfo', 'key2'];
let subscriberId = '11';
let result: Array<dataShare.OperationResult> = dataShareHelper.on('publishedDataChange', uris, subscriberId, onPublishCallback);
```
**示例**
```ts
import rpc from '@ohos.rpc';
let ashmem = null;
let subscriberId = '11';
let version = 1;
let data : Array<dataShare.PublishedItem> = [
{key:"city", subscriberId:"11", data:"xian"},
{key:"datashareproxy://com.acts.ohos.data.datasharetest/appInfo", subscriberId:"11", data:"appinfo is just a test app"},
{key:"empty", subscriberId:"11", data:"nobody sub"}];
let nums:number[] = [1,2,3];
function publishCallback(err, result: Array<dataShare.OperationResult>) {
console.info("publishCallback " + JSON.stringify(result));
ashmem.closeAshmem();
}
try {
ashmem = rpc.Ashmem.create("ashmem", (nums.length) * 4);
ashmem.mapReadWriteAshmem();
ashmem.writeAshmem(nums, nums.length, 0);
data.push({
"key" : "key2",
"data" : ashmem,
"subscriberId" : "11",
});
console.info("data length is:", data.length);
dataShareHelper.publish(data, "com.acts.ohos.data.datasharetest", version, publishCallback);
} catch (e) {
console.error("publish error " + JSON.stringify(e));
}
```
变更后代码示例:
**示例:** **示例:**
```ts ```ts
import rpc from '@ohos.rpc';
function onPublishCallback(err, node:dataShare.PublishedDataChangeNode) { function onPublishCallback(err, node:dataShare.PublishedDataChangeNode) {
console.info("onPublishCallback node bundleName " + JSON.stringify(node.bundleName));
console.info("onPublishCallback node data size" + node.data.length);
for (let i = 0; i < node.data.length; i++) { for (let i = 0; i < node.data.length; i++) {
console.info("onPublishCallback node " + typeof node.data[i].data);
if (typeof node.data[i].data != 'string') { if (typeof node.data[i].data != 'string') {
let array:ArrayBuffer = node.data[i].data; let array:ArrayBuffer = node.data[i].data;
let data:Uint8Array = new Uint8Array(array); let data:Uint8Array = new Uint8Array(array);
console.info("onPublishCallback " + i + " " + JSON.stringify(data)); console.info("onPublishCallback " + JSON.stringify(data));
} }
console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i]));
} }
} }
let uris:Array<string> = ['city', 'datashareproxy://com.acts.ohos.data.datasharetest/appInfo', 'key2']; let uris:Array<string> = ['city', 'datashareproxy://com.acts.ohos.data.datasharetest/appInfo', 'key2'];
let subscriberId = '11'; let subscriberId = '11';
let result: Array<dataShare.OperationResult> = dataShareHelper.on('publishedDataChange', uris, subscriberId, onPublishCallback); let result: Array<dataShare.OperationResult> = dataShareHelper.on('publishedDataChange', uris, subscriberId, onPublishCallback);
```
**示例:**
```ts
let arrayBuffer = null;
let subscriberId = '11';
let version = 1;
let data : Array<dataShare.PublishedItem> = [
{key:"city", subscriberId:"11", data:"xian"},
{key:"datashareproxy://com.acts.ohos.data.datasharetest/appInfo", subscriberId:"11", data:"appinfo is just a test app"},
{key:"empty", subscriberId:"11", data:"nobody sub"}];
let nums:number[] = [1,2,3];
function publishCallback(err, result: Array<dataShare.OperationResult>) {
console.info("publishCallback " + JSON.stringify(result));
}
try {
arrayBuffer = new ArrayBuffer(nums.length);
let array:Uint8Array = new Uint8Array(arrayBuffer);
data.push({
"key" : "key2",
"data" : array,
"subscriberId" : "11",
});
console.info("data length is:", data.length);
dataShareHelper.publish(data, "com.acts.ohos.data.datasharetest", version, publishCallback);
} catch (e) {
console.error("publish error " + JSON.stringify(e));
}
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册