未验证 提交 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 @@
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';
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|
| Name | Type | Readable | Writable | Description |
| ---------------- | ------ | -------- | -------- | ----------------------------------------------------------- |
| 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. |
## data_storage.getStorageSync
......@@ -35,25 +34,33 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value**
| Type| Description|
| -------- | -------- |
| [Storage](#storage) | **Storage** instance used for data storage operations.|
| Type | Description |
| ------------------- | ------------------------------------------------------ |
| [Storage](#storage) | **Storage** instance used for data storage operations. |
**Example**
```js
import data_storage from '@ohos.data.storage'
let path = '/data/storage/el2/database'
let storage = data_storage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
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
......@@ -65,25 +72,33 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Example**
```js
import data_storage from '@ohos.data.storage'
let path = '/data/storage/el2/database'
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()
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
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
......@@ -95,29 +110,37 @@ Reads the specified file and loads its data to the **Storage** instance for data
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|
| Type | Description |
| ---------------------------------- | ---------------------------------- |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result. |
**Example**
```js
import data_storage from '@ohos.data.storage'
let path = '/data/storage/el2/database'
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')
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
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
......@@ -129,15 +152,25 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Example**
```js
let path = '/data/storage/el2/database'
data_storage.deleteStorageSync(path + '/mystore')
```
```js
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
......@@ -149,22 +182,32 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| path | string | Yes | Path of the target file. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
```js
let path = '/data/storage/el2/database'
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) {
console.info("Deleted failed with err: " + err)
return
}
console.info("Deleted successfully.")
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
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
......@@ -176,25 +219,35 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let path = '/data/storage/el2/database'
let promisedelSt = data_storage.deleteStorage(path + '/mystore')
promisedelSt.then(() => {
console.info("Deleted successfully.")
}).catch((err) => {
console.info("Deleted failed with err: " + err)
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
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
......@@ -206,15 +259,25 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Example**
```js
let path = '/data/storage/el2/database'
data_storage.removeStorageFromCacheSync(path + '/mystore')
```
```js
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
......@@ -226,22 +289,32 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| path | string | Yes | Path of the target file. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
```js
let path = '/data/storage/el2/database'
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) {
console.info("Removed storage from cache failed with err: " + err)
return
}
console.info("Removed storage from cache successfully.")
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
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
......@@ -253,25 +326,36 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes | Path of the target file. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let path = '/data/storage/el2/database'
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
console.info("Removed storage from cache successfully.")
}).catch((err) => {
console.info("Removed storage from cache failed with err: " + err)
})
```
```js
import featureAbility from '@ohos.ability.featureAbility';
var path;
var context = featureAbility.getContext();
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
......@@ -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
**Parameters**
| 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.|
| 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. |
**Return value**
| 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.|
| 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. |
**Example**
```js
let value = storage.getSync('startup', 'default')
console.info("The value of startup is " + value)
```
```js
let value = storage.getSync('startup', 'default');
console.info("The value of startup is " + value);
```
### get
......@@ -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
**Parameters**
| 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.|
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|
| 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. |
| callback | AsyncCallback&lt;ValueType&gt; | Yes | Callback used to return the execution result. |
**Example**
```js
storage.get('startup', 'default', function(err, value) {
if (err) {
console.info("Get the value of startup failed with err: " + err)
return
```js
storage.get('startup', 'default', function(err, value) {
if (err) {
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
......@@ -342,25 +431,26 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**Parameters**
| 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.|
| 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. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;ValueType&gt; | Promise used to return the result.|
| Type | Description |
| ------------------------ | ---------------------------------- |
| Promise&lt;ValueType&gt; | Promise used to return the result. |
**Example**
```js
let promiseget = storage.get('startup', 'default')
promiseget.then((value) => {
console.info("The value of startup is " + value)
}).catch((err) => {
console.info("Get the value of startup failed with err: " + err)
})
```
```js
let promiseget = storage.get('startup', 'default');
promiseget.then((value) => {
console.info("The value of startup is " + value)
}).catch((err) => {
console.info("Failed to get the value of startup with err: " + err);
})
```
### putSync
......@@ -372,15 +462,17 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Example**
```js
storage.putSync('startup', 'auto')
```
```js
storage.putSync('startup', 'auto')
```
### put
......@@ -392,22 +484,24 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
| 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. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
```js
storage.put('startup', 'auto', function (err) {
if (err) {
console.info("Put the value of startup failed with err: " + err)
return
}
console.info("Put the value of startup successfully.")
})
```
```js
storage.put('startup', 'auto', function (err) {
if (err) {
console.info("Failed to put the value of startup with err: " + err);
return;
}
console.info("Succeeded in putting the value of startup.");
})
```
### put
......@@ -419,25 +513,27 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let promiseput = storage.put('startup', 'auto')
promiseput.then(() => {
console.info("Put the value of startup successfully.")
}).catch((err) => {
console.info("Put the value of startup failed with err: " + err)
})
```
```js
let promiseput = storage.put('startup', 'auto');
promiseput.then(() => {
console.info("Succeeded in putting the value of startup.");
}).catch((err) => {
console.info("Failed to put the value of startup with err: " + err);
})
```
### hasSync
......@@ -449,22 +545,25 @@ Checks whether the storage object contains data with a given key.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
| Type | Description |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
**Example**
```js
let isExist = storage.hasSync('startup')
if (isExist) {
console.info("The key of startup is contained.")
}
```
```js
let isExist = storage.hasSync('startup');
if (isExist) {
console.info("The key of startup is contained.");
}
```
### has
......@@ -476,28 +575,31 @@ Checks whether the storage object contains data with a given key. This API uses
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
| Type | Description |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
**Example**
```js
storage.has('startup', function (err, isExist) {
if (err) {
console.info("Check the key of startup failed with err: " + err)
return
}
if (isExist) {
console.info("The key of startup is contained.")
}
})
```
```js
storage.has('startup', function (err, isExist) {
if (err) {
console.info("Failed to check the key of startup with err: " + err);
return;
}
if (isExist) {
console.info("The key of startup is contained.");
}
})
```
### has
......@@ -509,26 +611,29 @@ Checks whether the storage object contains data with a given key. This API uses
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the result.|
| Type | Description |
| ---------------------- | ---------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result. |
**Example**
```js
let promisehas = storage.has('startup')
promisehas.then((isExist) => {
if (isExist) {
console.info("The key of startup is contained.")
}
}).catch((err) => {
console.info("Check the key of startup failed with err: " + err)
})
```
```js
let promisehas = storage.has('startup')
promisehas.then((isExist) => {
if (isExist) {
console.info("The key of startup is contained.");
}
}).catch((err) => {
console.info("Failed to check the key of startup with err: " + err);
})
```
### deleteSync
......@@ -540,14 +645,16 @@ Deletes data with the specified key from this storage object.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. |
**Example**
```js
storage.deleteSync('startup')
```
```js
storage.deleteSync('startup')
```
### delete
......@@ -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
**Parameters**
| 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.|
| 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. |
**Example**
```js
storage.delete('startup', function (err) {
if (err) {
console.info("Delete startup key failed with err: " + err)
return
}
console.info("Deleted startup key successfully.")
})
```
```js
storage.delete('startup', function (err) {
if (err) {
console.info("Failed to delete startup key failed err: " + err);
return;
}
console.info("Succeeded in deleting startup key.");
})
```
### delete
......@@ -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
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ---------------- |
| key | string | Yes | Key of the data. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let promisedel = storage.delete('startup')
promisedel.then(() => {
console.info("Deleted startup key successfully.")
}).catch((err) => {
console.info("Delete startup key failed with err: " + err)
})
```
```js
let promisedel = storage.delete('startup')
promisedel.then(() => {
console.info("Succeeded in deleting startup key.");
}).catch((err) => {
console.info("Failed to delete startup key failed err: " + err);
})
```
### flushSync
......@@ -614,9 +726,10 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Example**
```js
storage.flushSync()
```
```js
storage.flushSync()
```
### flush
......@@ -628,20 +741,22 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
```js
storage.flush(function (err) {
if (err) {
console.info("Flush to file failed with err: " + err)
return
}
console.info("Flushed to file successfully.")
})
```
```js
storage.flush(function (err) {
if (err) {
console.info("Failed to flush to file with err: " + err);
return;
}
console.info("Succeeded in flushing to file.");
})
```
### flush
......@@ -653,19 +768,21 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let promiseflush = storage.flush()
promiseflush.then(() => {
console.info("Flushed to file successfully.")
}).catch((err) => {
console.info("Flush to file failed with err: " + err)
})
```
```js
let promiseflush = storage.flush();
promiseflush.then(() => {
console.info("Succeeded in flushing to file.");
}).catch((err) => {
console.info("Failed to flush to file with err: " + err);
})
```
### clearSync
......@@ -677,9 +794,10 @@ Clears this **Storage** object.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Example**
```js
storage.clearSync()
```
```js
storage.clearSync()
```
### clear
......@@ -691,20 +809,22 @@ Clears this **Storage** object. This API uses an asynchronous callback to return
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
```js
storage.clear(function (err) {
if (err) {
console.info("Clear to file failed with err: " + err)
return
}
console.info("Cleared to file successfully.")
})
```
```js
storage.clear(function (err) {
if (err) {
console.info("Failed to clear the storage with err: " + err);
return;
}
console.info("Succeeded in clearing the storage.");
})
```
### clear
......@@ -716,19 +836,21 @@ Clears this **Storage** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise that returns no value.|
| Type | Description |
| ------------------- | ------------------------------ |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
```js
let promiseclear = storage.clear()
promiseclear.then(() => {
console.info("Cleared to file successfully.")
}).catch((err) => {
console.info("Clear to file failed with err: " + err)
})
```
```js
let promiseclear = storage.clear();
promiseclear.then(() => {
console.info("Succeeded in clearing the storage.");
}).catch((err) => {
console.info("Failed to clear the storage with err: " + err);
})
```
### on('change')
......@@ -740,20 +862,22 @@ Subscribes to data changes. The **StorageObserver** needs to be implemented. Whe
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Example**
```js
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
storage.on('change', observer)
storage.putSync('startup', 'auto')
storage.flushSync() // observer will be called.
```
```js
var observer = function (key) {
console.info("The key of " + key + " changed.");
}
storage.on('change', observer);
storage.putSync('startup', 'auto');
storage.flushSync(); // observer will be called.
```
### off('change')
......@@ -765,27 +889,29 @@ Unsubscribes from data changes.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| 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.|
| 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. |
**Example**
```js
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
storage.off('change', observer)
```
```js
var observer = function (key) {
console.info("The key of " + key + " changed.");
}
storage.off('change', observer);
```
## StorageObserver
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | No| Data changed.|
| Name | Type | Mandatory | Description |
| ---- | ------ | --------- | ------------- |
| key | string | No | Data changed. |
## ValueType
......@@ -793,8 +919,8 @@ Enumerates the value types.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Type | Description |
| ------- | -------------------- |
| number | The value is a number. |
| string | The value is a string. |
| boolean | The value is of Boolean type.|
| Type | Description |
| ------- | ----------------------------- |
| number | The value is a number. |
| string | The value is a string. |
| 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.
先完成此消息的编辑!
想要评论请 注册