提交 5f135b38 编写于 作者: A annie_wangli

update docs

Signed-off-by: Nannie_wangli <annie.wangli@huawei.com>
上级 fcbcccd5
# Distributed Object # Distributed Data Object
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> The initial APIs of this module are supported since API version 8. 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 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
...@@ -17,16 +17,21 @@ import distributedObject from '@ohos.data.distributedDataObject' ...@@ -17,16 +17,21 @@ import distributedObject from '@ohos.data.distributedDataObject'
createDistributedObject(source: object): DistributedObject createDistributedObject(source: object): DistributedObject
Creates a **distributedObject** instance. You can specify the attribute of the instance in **source**. This method returns the **distributedObject** instance created. Creates a distributed data object.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- **Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| object | source | Yes| Attribute of the **distributedObject** instance to create.| | source | object | Yes| Attribute of the distributed data object to create.|
- Example **Return Value**
| Type| Description|
| -------- | -------- |
| [DistributedObject](#distributedobject) | Distributed data object created.|
**Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
// Create a distributedObject instance. The attribute type of the object can be string, number, boolean, or Object. // Create a distributedObject instance. The attribute type of the object can be string, number, boolean, or Object.
...@@ -39,16 +44,16 @@ Creates a **distributedObject** instance. You can specify the attribute of the i ...@@ -39,16 +44,16 @@ Creates a **distributedObject** instance. You can specify the attribute of the i
genSessionId(): string genSessionId(): string
Creates a session ID randomly. Creates a random session ID.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- Return value **Return Value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| string | Session ID created.| | string | Session ID created.|
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
var sessionId = distributedObject.genSessionId(); var sessionId = distributedObject.genSessionId();
...@@ -58,20 +63,29 @@ Creates a session ID randomly. ...@@ -58,20 +63,29 @@ Creates a session ID randomly.
## DistributedObject ## DistributedObject
Represents a **distributedObject** instance. Represents a **distributedObject** instance.
### setSessionId ### setSessionId
setSessionId(sessionId?: string): boolean setSessionId(sessionId?: string): boolean
Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network. To remove a device from the distributed network, set this parameter to "" or leave it unspecified. If **true** is returned, the session ID is set successfully. Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
**Parameters**
- **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| sessionId | string | No| ID of a distributed object on a trusted network.| | sessionId | string | No| ID of a distributed data object on a trusted network. To remove a distributed data object from the network, set this parameter to "" or leave it empty.|
**Return Value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the session ID is set successfully;<br>returns **false** otherwise. |
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
...@@ -87,17 +101,17 @@ Sets a session ID for synchronization. Automatic synchronization is performed fo ...@@ -87,17 +101,17 @@ Sets a session ID for synchronization. Automatic synchronization is performed fo
on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void
Subscribes to the data changes of this distributed object. Subscribes to the data changes of this distributed data object.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- **Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data change events.| | type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data change events.|
| callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | Yes| Callback used to return the data changes of the distributed object. In the callback, **sessionId** indicates the session ID of the distributed object, and **fields** indicates the attributes of the object.| | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | Yes| Callback used to return the changes of the distributed data object.<br>**sessionId** indicates the session ID of the distributed data object.<br>**fields** indicates the attributes of the distributed data object changed.|
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
...@@ -116,18 +130,18 @@ Subscribes to the data changes of this distributed object. ...@@ -116,18 +130,18 @@ Subscribes to the data changes of this distributed object.
off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void
Unsubscribes from the data changes of this distributed object. Unsubscribes from the data changes of this distributed data object.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- **Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data change events.| | type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data change events.|
| callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | No| Callback used to return changes of the distributed object. If this parameter is not specified, all callbacks related to data changes will be unregistered.| | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | No| Callback used to return changes of the distributed data object. If this parameter is not specified, all callbacks related to data changes will be unregistered.<br>**sessionId** indicates the session ID of the distributed data object.<br>**fields** indicates the attributes of the distributed data object changed.|
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
...@@ -135,7 +149,7 @@ Unsubscribes from the data changes of this distributed object. ...@@ -135,7 +149,7 @@ Unsubscribes from the data changes of this distributed object.
g_object.on("change", function (sessionId, changeData) { g_object.on("change", function (sessionId, changeData) {
console.info("change" + sessionId); console.info("change" + sessionId);
}); });
// Unsubscribe from the data change callback for the specified distributed object. // Unsubscribe from the data change callback for the specified distributed data object.
g_object.off("change", function (sessionId, changeData) { g_object.off("change", function (sessionId, changeData) {
console.info("change" + sessionId); console.info("change" + sessionId);
}); });
...@@ -147,17 +161,19 @@ Unsubscribes from the data changes of this distributed object. ...@@ -147,17 +161,19 @@ Unsubscribes from the data changes of this distributed object.
on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, status: 'online' | 'offline' }>): void on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, status: 'online' | 'offline' }>): void
Subscribes to the status changes (online or offline) of this distributed object. Subscribes to the status changes (online or offline) of this distributed data object.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- **Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.| | type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.|
| callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | Yes| Callback used to return the status changes of the distributed object. In the callback, **sessionId** indicates the session ID of the distributed object, and **status** indicates the online or offline status of the distributed object.| | callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | Yes| Callback used to return the online or offline status.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** indicates the network ID of the device.<br>**status** indicates the status, which can be online or offline.|
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
...@@ -172,24 +188,24 @@ Subscribes to the status changes (online or offline) of this distributed object. ...@@ -172,24 +188,24 @@ Subscribes to the status changes (online or offline) of this distributed object.
off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, status: 'online' | 'offline' }>): void off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, status: 'online' | 'offline' }>): void
Unsubscribes from the status (online or offline) changes of the distributed object. Unsubscribes from the status (online or offline) changes of the distributed data object.
** System Capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
- **Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.| | type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.|
| callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | No| Callback used to return the status changes. If this parameter is not specified, all the status change callbacks will be unregistered.| | callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback used to return the status changes. If this parameter is not specified, all the status change callbacks will be unregistered.<br>**sessionId** indicates the session ID of the distributed data object.<br>**deviceId** indicates the device ID of the distributed data object.<br>**status** indicates the status, which can be online or offline.|
- Example **Example**
```js ```js
import distributedObject from '@ohos.data.distributedDataObject' import distributedObject from '@ohos.data.distributedDataObject'
g_object.on("status", function (sessionId, networkId, status) { g_object.on("status", function (sessionId, networkId, status) {
this.response += "status changed " + sessionId + " " + status + " " + networkId; this.response += "status changed " + sessionId + " " + status + " " + networkId;
}); });
// Unsubscribe from the status change callback for the specified distributed object. // Unsubscribe from the status change callback for the specified distributed data object.
g_object.off("status", function (sessionId, networkId, status) { g_object.off("status", function (sessionId, networkId, status) {
this.response += "status changed " + sessionId + " " + status + " " + networkId; this.response += "status changed " + sessionId + " " + status + " " + networkId;
}); });
......
...@@ -4,7 +4,10 @@ Lightweight storage provides applications with data processing capability and al ...@@ -4,7 +4,10 @@ Lightweight storage provides applications with data processing capability and al
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. <br/>The APIs of this module are no longer maintained since API Version 9. You are advised to use [@ohos.data.preferences](js-apis-data-preferences.md). >
> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> - The APIs of this module are no longer maintained since API Version 9. You are advised to use [`@ohos.data.preferences`](js-apis-data-preferences.md).
## Modules to Import ## Modules to Import
...@@ -45,12 +48,18 @@ Reads a file and loads the data to the **Storage** instance in synchronous mode. ...@@ -45,12 +48,18 @@ Reads a file and loads the data to the **Storage** instance in synchronous mode.
``` ```
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() var context = featureAbility.getContext()
var path = await context.getFilesDir() context.getFilesDir((err, path) => {
let storage = dataStorage.getStorageSync(path + '/mystore') if (err) {
storage.putSync('startup', 'auto') console.error('getFilesDir failed. err: ' + JSON.stringify(err));
storage.flushSync() return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let storage = dataStorage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()
});
``` ```
...@@ -72,17 +81,23 @@ Reads a file and loads the data to the **Storage** instance. This method uses an ...@@ -72,17 +81,23 @@ Reads a file and loads the data to the **Storage** instance. This method uses an
``` ```
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() var context = featureAbility.getContext()
var path = await context.getFilesDir() context.getFilesDir((err, path) => {
dataStorage.getStorage(path + '/mystore', function (err, storage) {
if (err) { if (err) {
console.info("Get the storage failed, path: " + path + '/mystore') console.error('getFilesDir failed. err: ' + JSON.stringify(err));
return; return;
} }
storage.putSync('startup', 'auto') console.info('getFilesDir successful. path:' + JSON.stringify(path));
storage.flushSync() dataStorage.getStorage(path + '/mystore', function (err, storage) {
}) if (err) {
console.info("Get the storage failed, path: " + path + '/mystore')
return;
}
storage.putSync('startup', 'auto')
storage.flushSync()
})
});
``` ```
...@@ -108,16 +123,22 @@ Reads a file and loads the data to the **Storage** instance. This method uses a ...@@ -108,16 +123,22 @@ Reads a file and loads the data to the **Storage** instance. This method uses a
``` ```
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() var context = featureAbility.getContext()
var path = await context.getFilesDir() context.getFilesDir((err, path) => {
let promisegetSt = dataStorage.getStorage(path + '/mystore') if (err) {
promisegetSt.then((storage) => { console.info("Get the storage failed, path: " + path + '/mystore')
storage.putSync('startup', 'auto') return;
storage.flushSync() }
}).catch((err) => { console.info('getFilesDir successful. path:' + JSON.stringify(path));
console.info("Get the storage failed, path: " + path + '/mystore') let promisegetSt = dataStorage.getStorage(path + '/mystore')
}) promisegetSt.then((storage) => {
storage.putSync('startup', 'auto')
storage.flushSync()
}).catch((err) => {
console.info("Get the storage failed, path: " + path + '/mystore')
})
});
``` ```
...@@ -250,7 +271,7 @@ removeStorageFromCache(path: string): Promise&lt;void&gt; ...@@ -250,7 +271,7 @@ removeStorageFromCache(path: string): Promise&lt;void&gt;
Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
This method uses a promise to return the result. This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -426,7 +447,7 @@ put(key: string, value: ValueType): Promise&lt;void&gt; ...@@ -426,7 +447,7 @@ put(key: string, value: ValueType): Promise&lt;void&gt;
Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**.
This method uses a promise to return the result. This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -522,7 +543,7 @@ has(key: string): Promise&lt;boolean&gt; ...@@ -522,7 +543,7 @@ has(key: string): Promise&lt;boolean&gt;
Checks whether the storage object contains data with a given key. Checks whether the storage object contains data with a given key.
This method uses a promise to return the result. This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -678,7 +699,7 @@ flush(): Promise&lt;void&gt; ...@@ -678,7 +699,7 @@ flush(): Promise&lt;void&gt;
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
This method uses a promise to return the result. This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -747,7 +768,7 @@ clear(): Promise&lt;void&gt; ...@@ -747,7 +768,7 @@ clear(): Promise&lt;void&gt;
Clears this **Storage** object. Clears this **Storage** object.
This method uses a promise to return the result. This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册