未验证 提交 4348946a 编写于 作者: O openharmony_ci 提交者: Gitee

!3027 #I4YLUR完成

Merge pull request !3027 from Annie_wang/PR2167
# Distributed Object
# Distributed Data Object
> ![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.
......@@ -17,16 +17,21 @@ import distributedObject from '@ohos.data.distributedDataObject'
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|
| -------- | -------- | -------- | -------- |
| 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
import distributedObject from '@ohos.data.distributedDataObject'
// 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
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|
| -------- | -------- |
| string | Session ID created.|
- Example
**Example**
```js
import distributedObject from '@ohos.data.distributedDataObject'
var sessionId = distributedObject.genSessionId();
......@@ -58,20 +63,29 @@ Creates a session ID randomly.
## DistributedObject
Represents a **distributedObject** instance.
### setSessionId
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|
| -------- | -------- | -------- | -------- |
| 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
import distributedObject from '@ohos.data.distributedDataObject'
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
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|
| -------- | -------- | -------- | -------- |
| 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
import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
......@@ -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
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|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to unsubscribe from. 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.|
| 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 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
import distributedObject from '@ohos.data.distributedDataObject'
var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,
......@@ -135,7 +149,7 @@ Unsubscribes from the data changes of this distributed object.
g_object.on("change", function (sessionId, changeData) {
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) {
console.info("change" + sessionId);
});
......@@ -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
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|
| -------- | -------- | -------- | -------- |
| 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
import distributedObject from '@ohos.data.distributedDataObject'
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.
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|
| -------- | -------- | -------- | -------- |
| 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
import distributedObject from '@ohos.data.distributedDataObject'
g_object.on("status", function (sessionId, networkId, status) {
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) {
this.response += "status changed " + sessionId + " " + status + " " + networkId;
});
......
......@@ -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/>
> 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
......@@ -47,10 +50,16 @@ Reads a file and loads the data to the **Storage** instance in synchronous mode.
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext()
var path = await context.getFilesDir()
context.getFilesDir((err, path) => {
if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err));
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let storage = dataStorage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()
});
```
......@@ -74,7 +83,12 @@ Reads a file and loads the data to the **Storage** instance. This method uses an
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext()
var path = await context.getFilesDir()
context.getFilesDir((err, path) => {
if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err));
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
dataStorage.getStorage(path + '/mystore', function (err, storage) {
if (err) {
console.info("Get the storage failed, path: " + path + '/mystore')
......@@ -83,6 +97,7 @@ Reads a file and loads the data to the **Storage** instance. This method uses an
storage.putSync('startup', 'auto')
storage.flushSync()
})
});
```
......@@ -110,7 +125,12 @@ Reads a file and loads the data to the **Storage** instance. This method uses a
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext()
var path = await context.getFilesDir()
context.getFilesDir((err, path) => {
if (err) {
console.info("Get the storage failed, path: " + path + '/mystore')
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let promisegetSt = dataStorage.getStorage(path + '/mystore')
promisegetSt.then((storage) => {
storage.putSync('startup', 'auto')
......@@ -118,6 +138,7 @@ Reads a file and loads the data to the **Storage** instance. This method uses a
}).catch((err) => {
console.info("Get the storage failed, path: " + path + '/mystore')
})
});
```
......@@ -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.
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
......@@ -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()**.
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
......@@ -522,7 +543,7 @@ has(key: string): Promise&lt;boolean&gt;
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
......@@ -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.
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
......@@ -747,7 +768,7 @@ clear(): Promise&lt;void&gt;
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册