提交 f92b17c5 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 8b683657
# DataShare Development
The **DataShare** module allows an application to manage its own data and share data with other applications. Currently, data can be shared only between the applications on the same device.
The **DataShare** module allows an application to manage its own data and share data with other applications. Currently, data can be shared only between applications on the same device.
## Available APIs
......@@ -29,10 +29,10 @@ For more details, see [DataShareHelper](../reference/apis/js-apis-data-dataShare
## When to Use
**DataShare** can be divided into the following:
There are two roles in **DataShare**.
- Data provider: Implement functions of adding, deleting, modifying, and querying data, and opening files, and share data.
- Data consumer: Access the data provided by the provider using **DataShareHelper**.
- Data provider: adds, deletes, modifies, and queries data, opens files, and shares data.
- Data consumer: accesses the data provided by the provider using **DataShareHelper**.
Examples are given below.
......@@ -47,7 +47,7 @@ Examples are given below.
import dataSharePredicates from '@ohos.data.dataSharePredicates'
```
2. Override **DataShareExtensionAbility** APIs based on actual requirements. For example, if the data provider provides only data query, override only the query() API.
2. Override **DataShareExtensionAbility** APIs based on actual requirements. For example, if the data provider provides only data query, override only the **query()** API.
3. Implement the data provider services. For example, implement data storage of the data provider by using a database, reading and writing files, or accessing the network.
......@@ -107,9 +107,9 @@ Examples are given below.
| Field| Description |
| ------------ | ------------------------------------------------------------ |
| "name" | Ability name, corresponding to the **ExtensionAbility** class name derived from **Ability**. |
| "type" | Ability type. The value is **dataShare**, indicating the development is based on the **Datashare** template.|
| "uri" | URI used for communication. It is the unique identifier for the client to connect to the server. |
| "visible" | Whether it is visible to other applications. Communication with other applications is allowed only when the value is **true**.|
| "type" | Ability type. The value is **dataShare**, indicating the development is based on the **datashare** template.|
| "uri" | URI used for communication. It is the unique identifier for the data consumer to connect to the provider. |
| "visible" | Whether it is visible to other applications. Data sharing is allowed only when the value is **true**.|
**module.json5 example**
......@@ -129,7 +129,7 @@ Examples are given below.
### Data Consumer Application Development
1. Import basic dependencies.
1. Import the dependencies.
```ts
import Ability from '@ohos.application.Ability'
......@@ -175,7 +175,7 @@ Examples are given below.
dsHelper.insert(dseUri, valuesBucket, (err,data) => {
console.log("dsHelper insert result: " + data);
});
// Delete the specified data.
// Delete data.
dsHelper.delete(dseUri, da, (err,data) => {
console.log("dsHelper delete result: " + data);
});
......
......@@ -2,9 +2,9 @@
## Introduction
The **DataShare** module allows an application to manage its own data and share data with other applications. Currently, data can be shared only between the applications on the same device.
The **DataShare** module allows an application to manage its own data and share data with other applications. Currently, data can be shared only between applications on the same device.
**DataShare** is used in application scenarios, such as contacts, short message service (SMS), and media gallery. However, not all data, such as accounts and passwords, can be accessed by other applications. Some data, such as SMS messages, can only be queried by other applications. **DataShare** provides a secure data sharing mechanism for applications.
Data needs to be shared in a wealth of scenarios. For example, contacts, short message service (SMS), and media gallery always needs to be shared. However, certain data, such as accounts and passwords, cannot be shared. Some data, such as SMS messages, can be queried but not modified by other applications. **DataShare** provides a secure data sharing mechanism for applications in a variety of scenarios.
The data provider can directly use the **DataShare** framework to share data with other applications without complex encapsulation. The data consumer only needs to learn and use a set of interfaces because the data access mode does not vary with the data provisioning mode. This greatly reduces the learning time and development difficulty.
......@@ -45,7 +45,7 @@ Before you get started, familiarize yourself with the following concepts:
- The **DataShareHelper** module, as the data consumer, provides interfaces for accessing data, including adding, deleting, modifying, and querying data.
- The data consumer communicates with the data provider using inter-process communication (IPC). The data provider can be implemented through a database or other data storage.
- The **ResultSet** module is implemented through shared memory. Share memory stores the result sets, and interfaces are provided to traverse result sets.
- The **ResultSet** module is implemented through shared memory. Shared memory stores the result sets, and interfaces are provided to traverse result sets.
## Constraints
......
......@@ -4,11 +4,11 @@ The **DataShareExtensionAbility** module provides Extension abilities for data s
>**NOTE**
>
>The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
>The APIs provided by this module are system APIs.
> The APIs provided by this module are system APIs.
>
>The APIs of this module can be used only in the stage model.
> The APIs of this module can be used only in the stage model.
## Modules to Import
......@@ -70,62 +70,6 @@ export default class DataShareExtAbility extends DataShareExtensionAbility {
};
```
## getFileTypes
getFileTypes?(uri: string, mimeTypeFilter: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Obtains the Multipurpose Internet Mail Extensions (MIME) types supported by a file. This API is called by the server and can be overridden as required.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
**Parameters**
| Name | Type | Mandatory| Description |
| -------------- | ---------------------------------------- | ---- | ---------------------------------- |
| uri | string | Yes | URI of the file. |
| mimeTypeFilter | string | Yes | MIME types to match. |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes | Callback invoked to return the MIME types obtained.|
**Example**
```ts
export default class DataShareExtAbility extends DataShareExtensionAbility {
getFileTypes(uri, mimeTypeFilter, callback) {
let err = {"code":0};
let ret = new Array("type01", "type02", "type03");
callback(err, ret);
}
};
```
## openFile
openFile?(uri: string, mode: string, callback: AsyncCallback&lt;number&gt;): void
Opens a file. This API is called by the server and can be overridden as required.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ------------------------------------------ |
| uri | string | Yes | URI of the file to open. |
| mode | string | Yes | File open mode, which can be read-only or read/write.|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked to return the file descriptor. |
**Example**
```ts
export default class DataShareExtAbility extends DataShareExtensionAbility {
openFile(uri, mode, callback) {
let err = {"code":0};
let fd = 0;
callback(err,fd);
}
};
```
## insert
insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback&lt;number&gt;): void
......@@ -301,33 +245,6 @@ export default class DataShareExtAbility extends DataShareExtensionAbility {
};
```
## getType
getType?(uri: string, callback: AsyncCallback&lt;string&gt;): void
Obtains the MIME type corresponding to the given URI. This API can be overridden as required.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
**Parameters**
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| uri | string | Yes | URI of the target data.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the MIME type obtained.|
**Example**
```ts
export default class DataShareExtAbility extends DataShareExtensionAbility {
getType(uri, callback) {
let err = {"code":0};
let ret = "image";
callback(err, ret);
}
};
```
## batchInsert
batchInsert?(uri: string, valueBuckets: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;number&gt;): void
......
......@@ -2,9 +2,11 @@
The **ValueBucket** module holds data in key-value (KV) pairs. You can use it to insert data into a database.
>**NOTE**
> **NOTE**
>
>The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> The APIs provided by this module are system APIs.
## Modules to Import
......
......@@ -2,9 +2,13 @@
The **DataShare** module allows an application to manage its own data and share data with other applications on the same device.
>**NOTE**
> **NOTE**
>
>The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> The APIs provided by this module are system APIs.
>
> The APIs of this module can be used only in the stage model.
## Modules to Import
......@@ -87,85 +91,12 @@ dataShare.createDataShareHelper(this.context, uri).then((data) => {
Provides a **DataShareHelper** instance to access or manage data on the server. Before calling an API provided by **DataShareHelper**, you must create a **DataShareHelper** instance using [createDataShareHelper](#datasharecreatedatasharehelper).
This API can be used only in the stage model.
### openFile
openFile(uri: string, mode: string, callback: AsyncCallback&lt;number&gt;): void
Opens a file. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------- |
| uri | string | Yes | URI of the file to open. |
| mode | string | Yes | File open mode.<br>**r** means to open a file for reading; **w** means to open a file for writing (erasing any data in the file); **wa** means to open a file in append mode for writing at the end of the file; **rw** means to open a file for both reading and writing.|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the file descriptor. Otherwise, **err** is an error object.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.openFile(uri, "rwt", (err, data) => {
if (err != undefined) {
console.info("openFile failed, error message : " + err);
}else {
console.info("openFile succeed, data : " + data);
let fd = data;
}
});
```
### openFile
openFile(uri: string, mode: string): Promise&lt;number&gt;
Opens a file. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name| Type | Mandatory| Description |
| ---- | ------ | ---- | ------------------------------------------------------------ |
| uri | string | Yes | URI of the file to open. |
| mode | string | Yes | File open mode.<br>**r** means to open a file for reading; **w** means to open a file for writing (erasing any data in the file); **wa** means to open a file in append mode for writing at the end of the file; **rw** means to open a file for both reading and writing.|
**Return value**
| Type | Description |
| --------------- | ---------------- |
| Promise&lt;number&gt; | Promise used to return the file descriptor.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.openFile(uri, "rwt").then((data) => {
console.info("openFile succeed, data : " + data);
let fd = data;
}).catch((err) => {
console.info("openFile failed, error message : " + err);
})
```
### on('dataChange')
on(type: 'dataChange', uri: string, callback: AsyncCallback&lt;void&gt;): void
Subscribes to changes of the specified data. After an observer is registered, the subscriber will receive a notification when the change notification is triggered (the **notifyChange()** method is called). This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -174,7 +105,7 @@ This API can be used only in the stage model.
| -------- | -------------------- | ---- | ------------------------ |
| type | string | Yes | Event type to subscribe to. The value is **dataChange**, which indicates data change events.|
| uri | string | Yes | URI of the data.|
| callback | AsyncCallback&lt;void&gt; | Yes | Called when the change notification is triggered. In this case, **err** is **undefined**. Otherwise, it is not called or **err** is an error object.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked to return the result. If the data is changed, the value of **err** is **undefined**. Otherwise, this callback is not invoked or the value of **err** is an error object.|
**Example**
......@@ -193,8 +124,6 @@ off(type: 'dataChange', uri: string, callback?: AsyncCallback&lt;void&gt;): void
Unsubscribes from the changes of the specified data. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -222,8 +151,6 @@ insert(uri: string, value: ValuesBucket, callback: AsyncCallback&lt;number&gt;):
Inserts a single data record into the database. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -259,8 +186,6 @@ insert(uri: string, value: ValuesBucket): Promise&lt;number&gt;
Inserts a single data record into the database. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -299,8 +224,6 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callbac
Deletes one or more data records from the database. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -335,8 +258,6 @@ delete(uri: string, predicates: dataSharePredicates.DataSharePredicates): Promis
Deletes one or more data records from the database. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -374,8 +295,6 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns:
Queries data in the database. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -412,8 +331,6 @@ query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns:
Queries data in the database. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -453,8 +370,6 @@ update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value:
Updates data in the database. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -496,8 +411,6 @@ update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value:
Updates data in the database. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -542,8 +455,6 @@ batchInsert(uri: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallb
Batch inserts data into the database. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -577,8 +488,6 @@ batchInsert(uri: string, values: Array&lt;ValuesBucket&gt;): Promise&lt;number&g
Batch inserts data into the database. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -609,149 +518,12 @@ dataShareHelper.batchInsert(uri, vbs).then((data) => {
});
```
### getType
getType(uri: string, callback: AsyncCallback&lt;string&gt;): void
Obtains the Multipurpose Internet Mail Extensions (MIME) type of the specified data. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------- | ---- | --------------------------------------------- |
| uri | string | Yes | URI of the data. |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the MIME type obtained. Otherwise, **err** is an error object.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.getType(uri, (err, data)=>{
if (err != undefined) {
console.log("getType failed, error message : " + err);
}else{
console.log("getType succeed, data : " + data);
let result = data;
}
});
```
### getType
getType(uri: string): Promise&lt;string&gt;
Obtains the MIME type of the specified data. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name| Type | Mandatory| Description |
| ---- | ------ | ---- | -------------------- |
| uri | string | Yes | URI of the data.|
**Return value**
| Type | Description |
| ---------------- | ----------------------------------- |
| Promise&lt;string&gt; | Promise used to return the MIME type obtained.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShareHelper.getType(uri).then((data) => {
console.log("getType succeed, data : " + data);
}).catch((err) => {
console.log("getType failed, error message : " + err);
});
```
### getFileTypes
getFileTypes(uri: string, mimeTypeFilter: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Obtains the supported MIME types. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name | Type | Mandatory| Description |
| -------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| uri | string | Yes | URI of the file. |
| mimeTypeFilter | string | Yes | MIME types to match. Example:<br>**\*/\***: Obtain all supported types.<br>**image/\***: Obtain the MIMEs with the main type of **image**.<br>**\*/jpg**: Obtain the MIMEs with the subtype of **jpg**.|
| callback | AsyncCallback<Array\<string>> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the MIME types obtained. Otherwise, **err** is an error object.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let mimeTypeFilter = "image/*";
dataShareHelper.getFileTypes(uri, mimeTypeFilter, (err,data) => {
if (err != undefined) {
console.log("getFileTypes failed, error message : " + err);
}else{
console.log("getFileTypes succeed, data : " + data);
}
});
```
### getFileTypes
getFileTypes(uri: string, mimeTypeFilter: string): Promise&lt;Array&lt;string&gt;&gt;
Obtains the supported MIME types. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
| Name | Type | Mandatory| Description |
| -------------- | ------ | ---- | ------------------------------------------------------------ |
| uri | string | Yes | URI of the file. |
| mimeTypeFilter | string | Yes | MIME types to match. Example:<br>**\*/\***: Obtain all supported types.<br>**image/\***: Obtain the MIMEs with the main type of **image**.<br>**\*/jpg**: Obtain the MIMEs with the subtype of **jpg**.|
**Return value**
| Type | Description |
| ------------------------ | ------------------------ |
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the MIME types obtained.|
**Example**
```ts
import Ability from '@ohos.application.Ability'
let uri = ("datashare:///com.samples.datasharetest.DataShare");
let mimeTypeFilter = "image/*";
dataShareHelper.getFileTypes(uri, mimeTypeFilter).then((data) => {
console.log("getFileTypes succeed, data : " + data);
}).catch((err) => {
console.log("getFileTypes failed, error message : " + err);
});
```
### normalizeUri
normalizeUri(uri: string, callback: AsyncCallback&lt;string&gt;): void
Normalizes a **DataShare** URI. The **DataShare** URI can be used only by the local device, but the normalized URI can be used across devices. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -781,8 +553,6 @@ normalizeUri(uri: string): Promise&lt;string&gt;
Normalizes a **DataShare** URI. The **DataShare** URI can be used only by the local device, but the normalized URI can be used across devices. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -815,8 +585,6 @@ denormalizeUri(uri: string, callback: AsyncCallback&lt;string&gt;): void
Denormalizes a URI. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -846,8 +614,6 @@ denormalizeUri(uri: string): Promise&lt;string&gt;
Denormalizes a URI. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -880,8 +646,6 @@ notifyChange(uri: string, callback: AsyncCallback&lt;void&gt;): void
Notifies the registered observer of data changes. This API uses an asynchronous callback to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......@@ -907,8 +671,6 @@ notifyChange(uri: string): Promise&lt;void&gt;
Notifies the registered observer of data changes. This API uses a promise to return the result.
This API can be used only in the stage model.
**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer
**Parameters**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册