未验证 提交 3a4a65ad 编写于 作者: O openharmony_ci 提交者: Gitee

!20573 modfiy datashare

Merge pull request !20573 from 李芮/master
......@@ -199,7 +199,7 @@ try {
| 名称 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 指定发布数据的键。 |
| data | string \| [Ashmem](js-apis-rpc.md#ashmem8) | 是 | 指定发布的数据。如果数据很大,请使用Ashmem。 |
| data | string \| ArrayBuffer | 是 | 指定发布的数据。如果发布数据大小超过20KB,建议使用ArrayBuffer。 |
| subscriberId | string | 是 | 指定订阅者id。 |
## RdbDataChangeNode<sup>10+</sup>
......@@ -478,18 +478,15 @@ on(type: 'publishedDataChange', uris: Array&lt;string&gt;, subscriberId: string,
**示例:**
```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();
let array:ArrayBuffer = node.data[i].data;
let data:Uint8Array = new Uint8Array(array);
console.info("onPublishCallback " + i + " " + JSON.stringify(data));
}
console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i]));
}
......@@ -561,29 +558,13 @@ publish(data: Array&lt;PublishedItem&gt;, bundleName: string, version: number, c
**示例:**
```ts
import rpc from '@ohos.rpc';
let ashmem = null;
let subscriberId = '11';
let arrayBuffer = new ArrayBuffer(1);
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];
let data : Array<dataShare.PublishedItem> = [{key:"key2", subscriberId:"11", data:arrayBuffer}];
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) {
......
# 分布式数据管理子系统ChangeLog
## cl.datashare.1 js-apis-data-dataShare API version 10开始托管数据PublishedItem中data的数据类型从Ashmem变更为ArrayBuffer
**变更影响**
PublishedItem中data的数据类型从Ashmem变更为ArrayBuffer
## PublishedItem<sup>10+</sup>
指定发布的数据类型。
**系统能力:** SystemCapability.DistributedDataManager.DataShare.Consumer
| 名称 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 指定发布数据的键。 |
| data | string \| ArrayBuffer | 是 | 指定发布的数据。如果数据很大,请使用ArrayBuffer。 |
| subscriberId | string | 是 | 指定订阅者id。 |
**适配指导**
示例代码如下:
**示例:**
```ts
let arrayBuffer = new ArrayBuffer(1);
let version = 1;
let data : Array<dataShare.PublishedItem> = [{key:"key2", subscriberId:"11", data:arrayBuffer}];
function publishCallback(err, result: Array<dataShare.OperationResult>) {
console.info("publishCallback " + JSON.stringify(result));
}
try {
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.
先完成此消息的编辑!
想要评论请 注册