未验证 提交 7b4595ed 编写于 作者: 葛亚芳 提交者: Gitee

update en/application-dev/reference/apis/js-apis-data-storage.md.

Signed-off-by: N@ge-yafang <geyafang@huawei.com>
Signed-off-by: N葛亚芳 <geyafang@huawei.com>
上级 4d484dd5
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type. Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
> **NOTE**<br/> > **NOTE**
> >
> 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 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.
...@@ -20,10 +19,10 @@ import data_storage from '@ohos.data.storage'; ...@@ -20,10 +19,10 @@ import data_storage from '@ohos.data.storage';
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Readable| Writable| Description| | Name | Type | Readable | Writable | Description |
| -------- | -------- | -------- | -------- | -------- | | ---------------- | ------ | -------- | -------- | ----------------------------------------------------------- |
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It must be less than 80 bytes.| | MAX_KEY_LENGTH | string | Yes | No | Maximum length of a key. It must be less than 80 bytes. |
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.| | MAX_VALUE_LENGTH | string | Yes | No | Maximum length of a value. It must be less than 8192 bytes. |
## data_storage.getStorageSync ## data_storage.getStorageSync
...@@ -35,25 +34,33 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -35,25 +34,33 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| [Storage](#storage) | **Storage** instance used for data storage operations.| | ------------------- | ------------------------------------------------------ |
| [Storage](#storage) | **Storage** instance used for data storage operations. |
**Example** **Example**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
let storage = data_storage.getStorageSync(path + '/mystore') var path;
storage.putSync('startup', 'auto') var context = featureAbility.getContext();
storage.flushSync() context.getFilesDir().then((filePath) => {
path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let storage = data_storage.getStorageSync(path + '/mystore');
storage.putSync('startup', 'auto');
storage.flushSync();
```
## data_storage.getStorage ## data_storage.getStorage
...@@ -65,25 +72,33 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -65,25 +72,33 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | -------- | ---------------------------------------- | --------- | --------------------------------------------- |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes| Callback used to return the execution result.| | path | string | Yes | Path of the target file. |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes | Callback used to return the execution result. |
**Example** **Example**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
data_storage.getStorage(path + '/mystore', function (err, storage) { var path;
if (err) { var context = featureAbility.getContext();
console.info("Failed to get the storage. Path: " + path + '/mystore') context.getFilesDir().then((filePath) => {
return; path = filePath;
} console.info("======================>getFilesDirPromsie====================>");
storage.putSync('startup', 'auto') });
storage.flushSync()
}) data_storage.getStorage(path + '/mystore', function (err, storage) {
``` if (err) {
console.info("Failed to get the storage. path: " + path + '/mystore');
return;
}
storage.putSync('startup', 'auto');
storage.flushSync();
})
```
## data_storage.getStorage ## data_storage.getStorage
...@@ -95,29 +110,37 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -95,29 +110,37 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.| | ---------------------------------- | ---------------------------------- |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result. |
**Example** **Example**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
var path;
let getPromise = data_storage.getStorage(path + '/mystore') var context = featureAbility.getContext();
getPromise.then((storage) => { context.getFilesDir().then((filePath) => {
storage.putSync('startup', 'auto') path = filePath;
storage.flushSync() console.info("======================>getFilesDirPromsie====================>");
}).catch((err) => { });
console.info("Failed to get the storage. Path: " + path + '/mystore')
}) let getPromise = data_storage.getStorage(path + '/mystore');
``` getPromise.then((storage) => {
storage.putSync('startup', 'auto');
storage.flushSync();
}).catch((err) => {
console.info("Failed to get the storage. path: " + path + '/mystore');
})
```
## data_storage.deleteStorageSync ## data_storage.deleteStorageSync
...@@ -129,15 +152,25 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -129,15 +152,25 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
data_storage.deleteStorageSync(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
```
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.deleteStorageSync(path + '/mystore');
```
## data_storage.deleteStorage ## data_storage.deleteStorage
...@@ -149,22 +182,32 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -149,22 +182,32 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | path | string | Yes | Path of the target file. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
data_storage.deleteStorage(path + '/mystore', function (err) { import featureAbility from '@ohos.ability.featureAbility';
if (err) {
console.info("Deleted failed with err: " + err) var path;
return var context = featureAbility.getContext();
} context.getFilesDir().then((filePath) => {
console.info("Deleted successfully.") path = filePath;
}) console.info("======================>getFilesDirPromsie====================>");
``` });
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) {
console.info("Failed to delete the storage with err: " + err);
return;
}
console.info("Succeeded in deleting the storage.");
})
```
## data_storage.deleteStorage ## data_storage.deleteStorage
...@@ -176,25 +219,35 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -176,25 +219,35 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value** **Return value**
| Type| Description| | Type | Description |
| -------- | -------- | | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value.| | Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
let promisedelSt = data_storage.deleteStorage(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
promisedelSt.then(() => {
console.info("Deleted successfully.") var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Deleted failed with err: " + err) context.getFilesDir().then((filePath) => {
}) path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let promisedelSt = data_storage.deleteStorage(path + '/mystore');
promisedelSt.then(() => {
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Failed to delete the storage with err: " + err);
})
```
## data_storage.removeStorageFromCacheSync ## data_storage.removeStorageFromCacheSync
...@@ -206,15 +259,25 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -206,15 +259,25 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
data_storage.removeStorageFromCacheSync(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
```
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.removeStorageFromCacheSync(path + '/mystore');
```
## data_storage.removeStorageFromCache ## data_storage.removeStorageFromCache
...@@ -226,22 +289,32 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -226,22 +289,32 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | path | string | Yes | Path of the target file. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
data_storage.removeStorageFromCache(path + '/mystore', function (err) { import featureAbility from '@ohos.ability.featureAbility';
if (err) {
console.info("Removed storage from cache failed with err: " + err) var path;
return var context = featureAbility.getContext();
} context.getFilesDir().then((filePath) => {
console.info("Removed storage from cache successfully.") path = filePath;
}) console.info("======================>getFilesDirPromsie====================>");
``` });
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) {
console.info("Failed to remove storage from cache with err: " + err);
return;
}
console.info("Succeeded in removing storage from cache.");
})
```
## data_storage.removeStorageFromCache ## data_storage.removeStorageFromCache
...@@ -253,25 +326,36 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -253,25 +326,36 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| path | string | Yes| Path of the target file.| | ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;void&gt; | Promise that returns no value.| | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js
let path = '/data/storage/el2/database' ```js
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
promiserevSt.then(() => {
console.info("Removed storage from cache successfully.") var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Removed storage from cache failed with err: " + err) context.getFilesDir().then((filePath) => {
}) path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
console.info("Succeeded in removing storage from cache.");
}).catch((err) => {
console.info("Failed to remove storage from cache with err: " + err);
})
```
## Storage ## Storage
...@@ -288,21 +372,24 @@ Obtains the value corresponding to a key. If the value is null or not in the def ...@@ -288,21 +372,24 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | -------- | ----------------------- | --------- | ------------------------------------------------------------ |
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value.| | key | string | Yes | Key of the data. It cannot be empty. |
| defValue | [ValueType](#valuetype) | Yes | Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.| | --------- | ------------------------------------------------------------ |
| ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned. |
**Example** **Example**
```js
let value = storage.getSync('startup', 'default') ```js
console.info("The value of startup is " + value) let value = storage.getSync('startup', 'default');
``` console.info("The value of startup is " + value);
```
### get ### get
...@@ -314,22 +401,24 @@ Obtains the value corresponding to a key. If the value is null or not in the def ...@@ -314,22 +401,24 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | -------- | ------------------------------ | --------- | ------------------------------------------------------------ |
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.| | key | string | Yes | Key of the data. It cannot be empty. |
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.| | defValue | [ValueType](#valuetype) | Yes | Default value to be returned. It can be a number, string, or Boolean value. |
| callback | AsyncCallback&lt;ValueType&gt; | Yes | Callback used to return the execution result. |
**Example** **Example**
```js
storage.get('startup', 'default', function(err, value) { ```js
if (err) { storage.get('startup', 'default', function(err, value) {
console.info("Get the value of startup failed with err: " + err) if (err) {
return console.info("Failed to get the value of startup with err: " + err);
return;
} }
console.info("The value of startup is " + value) console.info("The value of startup is " + value);
}) })
``` ```
### get ### get
...@@ -342,25 +431,26 @@ Obtains the value corresponding to a key. If the value is null or not in the def ...@@ -342,25 +431,26 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- | | -------- | ----------------------- | --------- | ------------------------------------------------------------ |
| key | string | Yes| Key of the data. It cannot be empty.| | key | string | Yes | Key of the data. It cannot be empty. |
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.| | defValue | [ValueType](#valuetype) | Yes | Default value to be returned. It can be a number, string, or Boolean value. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;ValueType&gt; | Promise used to return the result.| | ------------------------ | ---------------------------------- |
| Promise&lt;ValueType&gt; | Promise used to return the result. |
**Example** **Example**
```js ```js
let promiseget = storage.get('startup', 'default') let promiseget = storage.get('startup', 'default');
promiseget.then((value) => { promiseget.then((value) => {
console.info("The value of startup is " + value) console.info("The value of startup is " + value)
}).catch((err) => { }).catch((err) => {
console.info("Get the value of startup failed with err: " + err) console.info("Failed to get the value of startup with err: " + err);
}) })
``` ```
### putSync ### putSync
...@@ -372,15 +462,17 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -372,15 +462,17 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | ----- | ----------------------- | --------- | ------------------------------------------------------------ |
| value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.| | key | string | Yes | Key of the data. It cannot be empty. |
| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value. |
**Example** **Example**
```js
storage.putSync('startup', 'auto') ```js
``` storage.putSync('startup', 'auto')
```
### put ### put
...@@ -392,22 +484,24 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -392,22 +484,24 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | -------- | ------------------------- | --------- | ------------------------------------------------------------ |
| value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.| | key | string | Yes | Key of the data. It cannot be empty. |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
storage.put('startup', 'auto', function (err) { ```js
if (err) { storage.put('startup', 'auto', function (err) {
console.info("Put the value of startup failed with err: " + err) if (err) {
return console.info("Failed to put the value of startup with err: " + err);
} return;
console.info("Put the value of startup successfully.") }
}) console.info("Succeeded in putting the value of startup.");
``` })
```
### put ### put
...@@ -419,25 +513,27 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -419,25 +513,27 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | ----- | ----------------------- | --------- | ------------------------------------------------------------ |
| value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.| | key | string | Yes | Key of the data. It cannot be empty. |
| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;void&gt; | Promise that returns no value.| | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js ```js
let promiseput = storage.put('startup', 'auto') let promiseput = storage.put('startup', 'auto');
promiseput.then(() => { promiseput.then(() => {
console.info("Put the value of startup successfully.") console.info("Succeeded in putting the value of startup.");
}).catch((err) => { }).catch((err) => {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup with err: " + err);
}) })
``` ```
### hasSync ### hasSync
...@@ -449,22 +545,25 @@ Checks whether the storage object contains data with a given key. ...@@ -449,22 +545,25 @@ Checks whether the storage object contains data with a given key.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.| | ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
**Example** **Example**
```js
let isExist = storage.hasSync('startup') ```js
if (isExist) { let isExist = storage.hasSync('startup');
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
``` }
```
### has ### has
...@@ -476,28 +575,31 @@ Checks whether the storage object contains data with a given key. This API uses ...@@ -476,28 +575,31 @@ Checks whether the storage object contains data with a given key. This API uses
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | -------- | ---------------------------- | --------- | --------------------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.| | key | string | Yes | Key of the data. It cannot be empty. |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return the execution result. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.| | ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
**Example** **Example**
```js
storage.has('startup', function (err, isExist) { ```js
if (err) { storage.has('startup', function (err, isExist) {
console.info("Check the key of startup failed with err: " + err) if (err) {
return console.info("Failed to check the key of startup with err: " + err);
} return;
if (isExist) { }
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
}) }
``` })
```
### has ### has
...@@ -509,26 +611,29 @@ Checks whether the storage object contains data with a given key. This API uses ...@@ -509,26 +611,29 @@ Checks whether the storage object contains data with a given key. This API uses
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;boolean&gt; | Promise used to return the result.| | ---------------------- | ---------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result. |
**Example** **Example**
```js
let promisehas = storage.has('startup') ```js
promisehas.then((isExist) => { let promisehas = storage.has('startup')
if (isExist) { promisehas.then((isExist) => {
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
}).catch((err) => { }
console.info("Check the key of startup failed with err: " + err) }).catch((err) => {
}) console.info("Failed to check the key of startup with err: " + err);
``` })
```
### deleteSync ### deleteSync
...@@ -540,14 +645,16 @@ Deletes data with the specified key from this storage object. ...@@ -540,14 +645,16 @@ Deletes data with the specified key from this storage object.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Example** **Example**
```js
storage.deleteSync('startup') ```js
``` storage.deleteSync('startup')
```
### delete ### delete
...@@ -559,21 +666,23 @@ Deletes data with the specified key from this storage object. This API uses an a ...@@ -559,21 +666,23 @@ Deletes data with the specified key from this storage object. This API uses an a
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data. It cannot be empty.| | -------- | ------------------------- | --------- | ------------------------------------ |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | key | string | Yes | Key of the data. It cannot be empty. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
storage.delete('startup', function (err) { ```js
if (err) { storage.delete('startup', function (err) {
console.info("Delete startup key failed with err: " + err) if (err) {
return console.info("Failed to delete startup key failed err: " + err);
} return;
console.info("Deleted startup key successfully.") }
}) console.info("Succeeded in deleting startup key.");
``` })
```
### delete ### delete
...@@ -585,24 +694,27 @@ Deletes data with the specified key from this storage object. This API uses a pr ...@@ -585,24 +694,27 @@ Deletes data with the specified key from this storage object. This API uses a pr
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| key | string | Yes| Key of the data.| | ---- | ------ | --------- | ---------------- |
| key | string | Yes | Key of the data. |
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;void&gt; | Promise that returns no value.| | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js
let promisedel = storage.delete('startup') ```js
promisedel.then(() => { let promisedel = storage.delete('startup')
console.info("Deleted startup key successfully.") promisedel.then(() => {
}).catch((err) => { console.info("Succeeded in deleting startup key.");
console.info("Delete startup key failed with err: " + err) }).catch((err) => {
}) console.info("Failed to delete startup key failed err: " + err);
``` })
```
### flushSync ### flushSync
...@@ -614,9 +726,10 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -614,9 +726,10 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Example** **Example**
```js
storage.flushSync() ```js
``` storage.flushSync()
```
### flush ### flush
...@@ -628,20 +741,22 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -628,20 +741,22 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
storage.flush(function (err) { ```js
if (err) { storage.flush(function (err) {
console.info("Flush to file failed with err: " + err) if (err) {
return console.info("Failed to flush to file with err: " + err);
} return;
console.info("Flushed to file successfully.") }
}) console.info("Succeeded in flushing to file.");
``` })
```
### flush ### flush
...@@ -653,19 +768,21 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -653,19 +768,21 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;void&gt; | Promise that returns no value.| | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js
let promiseflush = storage.flush() ```js
promiseflush.then(() => { let promiseflush = storage.flush();
console.info("Flushed to file successfully.") promiseflush.then(() => {
}).catch((err) => { console.info("Succeeded in flushing to file.");
console.info("Flush to file failed with err: " + err) }).catch((err) => {
}) console.info("Failed to flush to file with err: " + err);
``` })
```
### clearSync ### clearSync
...@@ -677,9 +794,10 @@ Clears this **Storage** object. ...@@ -677,9 +794,10 @@ Clears this **Storage** object.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Example** **Example**
```js
storage.clearSync() ```js
``` storage.clearSync()
```
### clear ### clear
...@@ -691,20 +809,22 @@ Clears this **Storage** object. This API uses an asynchronous callback to return ...@@ -691,20 +809,22 @@ Clears this **Storage** object. This API uses an asynchronous callback to return
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | Name | Type | Mandatory | Description |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.| | -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example** **Example**
```js
storage.clear(function (err) { ```js
if (err) { storage.clear(function (err) {
console.info("Clear to file failed with err: " + err) if (err) {
return console.info("Failed to clear the storage with err: " + err);
} return;
console.info("Cleared to file successfully.") }
}) console.info("Succeeded in clearing the storage.");
``` })
```
### clear ### clear
...@@ -716,19 +836,21 @@ Clears this **Storage** object. This API uses a promise to return the result. ...@@ -716,19 +836,21 @@ Clears this **Storage** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value** **Return value**
| Type| Description|
| -------- | -------- | | Type | Description |
| Promise&lt;void&gt; | Promise that returns no value.| | ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example** **Example**
```js
let promiseclear = storage.clear() ```js
promiseclear.then(() => { let promiseclear = storage.clear();
console.info("Cleared to file successfully.") promiseclear.then(() => {
}).catch((err) => { console.info("Succeeded in clearing the storage.");
console.info("Clear to file failed with err: " + err) }).catch((err) => {
}) console.info("Failed to clear the storage with err: " + err);
``` })
```
### on('change') ### on('change')
...@@ -740,20 +862,22 @@ Subscribes to data changes. The **StorageObserver** needs to be implemented. Whe ...@@ -740,20 +862,22 @@ Subscribes to data changes. The **StorageObserver** needs to be implemented. Whe
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Description|
| -------- | -------- | -------- | | Name | Type | Description |
| type | string | Event type. The value **change** indicates data change events.| | -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.| | type | string | Event type. The value **change** indicates data change events. |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes. |
**Example** **Example**
```js
var observer = function (key) { ```js
console.info("The key of " + key + " changed.") var observer = function (key) {
} console.info("The key of " + key + " changed.");
storage.on('change', observer) }
storage.putSync('startup', 'auto') storage.on('change', observer);
storage.flushSync() // observer will be called. storage.putSync('startup', 'auto');
``` storage.flushSync(); // observer will be called.
```
### off('change') ### off('change')
...@@ -765,27 +889,29 @@ Unsubscribes from data changes. ...@@ -765,27 +889,29 @@ Unsubscribes from data changes.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type| Description|
| -------- | -------- | -------- | | Name | Type | Description |
| type | string | Event type. The value **change** indicates data change events.| | -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.| | type | string | Event type. The value **change** indicates data change events. |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes. |
**Example** **Example**
```js
var observer = function (key) { ```js
console.info("The key of " + key + " changed.") var observer = function (key) {
} console.info("The key of " + key + " changed.");
storage.off('change', observer) }
``` storage.off('change', observer);
```
## StorageObserver ## StorageObserver
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Mandatory| Description| | Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- | | ---- | ------ | --------- | ------------- |
| key | string | No| Data changed.| | key | string | No | Data changed. |
## ValueType ## ValueType
...@@ -793,8 +919,8 @@ Enumerates the value types. ...@@ -793,8 +919,8 @@ Enumerates the value types.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Type | Description | | Type | Description |
| ------- | -------------------- | | ------- | ----------------------------- |
| number | The value is a number. | | number | The value is a number. |
| string | The value is a string. | | string | The value is a string. |
| boolean | The value is of Boolean type.| | boolean | The value is of Boolean type. |
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册