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

!8197 3.1Release分支:data-storage 和window问题修改:window需同步翻译

Merge pull request !8197 from 葛亚芳/OpenHarmony-3.1-Release
...@@ -76,8 +76,8 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -76,8 +76,8 @@ Use the following APIs to delete a **Storage** instance or data file.
1. Import **@ohos.data.storage** and related modules to the development environment. 1. Import **@ohos.data.storage** and related modules to the development environment.
```js ```js
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility' // Used to obtain the file storage path. import featureAbility from '@ohos.ability.featureAbility'; // Used to obtain the file storage path.
``` ```
2. Create a **Storage** instance. 2. Create a **Storage** instance.
...@@ -85,9 +85,14 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -85,9 +85,14 @@ Use the following APIs to delete a **Storage** instance or data file.
Read the specified file and load its data to the **Storage** instance for data operations. Read the specified file and load its data to the **Storage** instance for data operations.
```js ```js
var context = featureAbility.getContext() var path;
var path = await context.getFilesDir() var context = featureAbility.getContext();
let promise = dataStorage.getStorage(path + '/mystore') context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
let promise = dataStorage.getStorage(path + '/mystore');
``` ```
...@@ -97,14 +102,14 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -97,14 +102,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js ```js
promise.then((storage) => { promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // Save data to the Storage instance. let getPromise = storage.put('startup', 'auto'); // Save data to the Storage instance.
getPromise.then(() => { getPromise.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);
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -115,14 +120,14 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -115,14 +120,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js ```js
promise.then((storage) => { promise.then((storage) => {
let getPromise = storage.get('startup', 'default') let getPromise = storage.get('startup', 'default');
getPromise.then((value) => { getPromise.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);
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -142,15 +147,15 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -142,15 +147,15 @@ Use the following APIs to delete a **Storage** instance or data file.
```js ```js
promise.then((storage) => { promise.then((storage) => {
var observer = function (key) { var observer = function (key) {
console.info("The key of " + key + " changed.") console.info("The key of " + key + " changed.");
} }
storage.on('change', observer) storage.on('change', observer);
storage.putSync('startup', 'auto') // Modify data in the Storage instance. storage.putSync('startup', 'auto'); // Modify data in the Storage instance.
storage.flushSync() // Trigger the StorageObserver callback. storage.flushSync(); // Trigger the StorageObserver callback.
storage.off(...change..., observer) // Unsubscribe from the data changes. storage.off('change', observer); // Unsubscribe from the data changes.
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -160,11 +165,11 @@ Use the following APIs to delete a **Storage** instance or data file. ...@@ -160,11 +165,11 @@ Use the following APIs to delete a **Storage** instance or data file.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored. Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
```js ```js
let promise = dataStorage.deleteStorage(path + '/mystore') let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => { promise.then(() => {
console.info("Deleted successfully.") console.info("Succeeded in deleting the storage.");
}).catch((err) => { }).catch((err) => {
console.info("Deleted failed with err: " + err) console.info("Failed to deleted the storage with err: " + err);
}) })
``` ```
...@@ -3,13 +3,12 @@ ...@@ -3,13 +3,12 @@
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.
## Modules to Import ## Modules to Import
```js ```js
...@@ -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|
| -------- | -------- |
| [Storage](#storage) | **Storage** instance used for data storage operations.|
**Example** | Type | Description |
```js | ------------------- | ------------------------------------------------------ |
import data_storage from '@ohos.data.storage' | [Storage](#storage) | **Storage** instance used for data storage operations. |
let path = '/data/storage/el2/database' **Example**
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 ## 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'
let path = '/data/storage/el2/database' ```js
data_storage.getStorage(path + '/mystore', function (err, storage) { 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) { if (err) {
console.info("Failed to get the storage. Path: " + path + '/mystore') console.info("Failed to get the storage. path: " + path + '/mystore');
return; return;
} }
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto');
storage.flushSync() 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|
| -------- | -------- |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|
**Example** | Type | Description |
```js | ---------------------------------- | ---------------------------------- |
import data_storage from '@ohos.data.storage' | Promise&lt;[Storage](#storage)&gt; | Promise used to return the result. |
let path = '/data/storage/el2/database' **Example**
let getPromise = data_storage.getStorage(path + '/mystore') ```js
getPromise.then((storage) => { import featureAbility from '@ohos.ability.featureAbility';
storage.putSync('startup', 'auto')
storage.flushSync() var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Failed to get the storage. Path: " + path + '/mystore') 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 ## 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';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Deleted failed with err: " + err) console.info("Failed to delete the storage with err: " + err);
return return;
} }
console.info("Deleted successfully.") 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';
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Removed storage from cache failed with err: " + err) console.info("Failed to remove storage from cache with err: " + err);
return return;
} }
console.info("Removed storage from cache successfully.") 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
storage.get('startup', 'default', function(err, value) {
if (err) { if (err) {
console.info("Get the value of startup failed with err: " + err) console.info("Failed to get the value of startup with err: " + err);
return 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
storage.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup with err: " + err);
return 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
storage.has('startup', function (err, isExist) {
if (err) { if (err) {
console.info("Check the key of startup failed with err: " + err) console.info("Failed to check the key of startup with err: " + err);
return return;
} }
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") 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')
promisehas.then((isExist) => {
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") console.info("The key of startup is contained.");
} }
}).catch((err) => { }).catch((err) => {
console.info("Check the key of startup failed with err: " + 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
storage.delete('startup', function (err) {
if (err) { if (err) {
console.info("Delete startup key failed with err: " + err) console.info("Failed to delete startup key failed err: " + err);
return 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
storage.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush to file with err: " + err);
return 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
storage.clear(function (err) {
if (err) { if (err) {
console.info("Clear to file failed with err: " + err) console.info("Failed to clear the storage with err: " + err);
return 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
...@@ -794,7 +920,7 @@ Enumerates the value types. ...@@ -794,7 +920,7 @@ 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
...@@ -75,20 +75,22 @@ ...@@ -75,20 +75,22 @@
1. 准备工作,导入@ohos.data.storage以及相关的模块到开发环境。 1. 准备工作,导入@ohos.data.storage以及相关的模块到开发环境。
```js ```js
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility' // 用于获取文件存储路径 import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径
``` ```
2. 获取Storage实例。 2. 获取Storage实例。
读取指定文件,将数据加载到Storage实例,用于数据操作。 读取指定文件,将数据加载到Storage实例,用于数据操作。
```js ```js
var context = featureAbility.getContext() var path;
context.getFilesDir().then(() => { var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>"); console.info("======================>getFilesDirPromsie====================>");
}); });
let promise = dataStorage.getStorage(path + '/mystore') let promise = dataStorage.getStorage(path + '/mystore');
``` ```
3. 存入数据。 3. 存入数据。
...@@ -97,14 +99,14 @@ ...@@ -97,14 +99,14 @@
```js ```js
promise.then((storage) => { promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // 保存数据到缓存的storage示例中。 let getPromise = storage.put('startup', 'auto'); // 保存数据到缓存的storage示例中。
getPromise.then(() => { getPromise.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 failed with err: " + err);
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -116,12 +118,14 @@ ...@@ -116,12 +118,14 @@
promise.then((storage) => { promise.then((storage) => {
let getPromise = storage.get('startup', 'default') let getPromise = storage.get('startup', 'default')
getPromise.then((value) => { getPromise.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);
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed")}) console.info("Failed to get the storage.")
})
``` ```
5. 数据持久化。 5. 数据持久化。
...@@ -139,15 +143,15 @@ ...@@ -139,15 +143,15 @@
```js ```js
promise.then((storage) => { promise.then((storage) => {
var observer = function (key) { var observer = function (key) {
console.info("The key of " + key + " changed.") console.info("The key of " + key + " changed.");
} }
storage.on('change', observer) storage.on('change', observer)
storage.putSync('startup', 'auto') // 修改storage存储数据 storage.putSync('startup', 'auto'); // 修改storage存储数据
storage.flushSync() // 触发订阅者回调方法 storage.flushSync(); // 触发订阅者回调方法
storage.off('change', observer) // 注销数据变化订阅 storage.off('change', observer); // 注销数据变化订阅
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -156,11 +160,13 @@ ...@@ -156,11 +160,13 @@
使用deleteStorage方法从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。 使用deleteStorage方法从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。
```js ```js
let promise = dataStorage.deleteStorage(path + '/mystore') let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => { promise.then(() => {
console.info("Deleted successfully.") console.info("Succeeded in deleting the storage.");
}).catch((err) => { }).catch((err) => {
console.info("Deleted failed with err: " + err)}) console.info("Failed to delete the storage with err: " + err);
})
``` ```
## 相关实例 ## 相关实例
针对轻量级数据存储开发,有以下相关实例可供参考: 针对轻量级数据存储开发,有以下相关实例可供参考:
......
...@@ -19,7 +19,7 @@ import data_storage from '@ohos.data.storage'; ...@@ -19,7 +19,7 @@ import data_storage from '@ohos.data.storage';
**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core
| 名称 | 参数类型 | 可读 | 可写 | 说明 | | 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- | | ---------------- | -------- | ---- | ---- | ------------------------------------- |
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,需小于80字节。 | | MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,需小于80字节。 |
| MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,需小于8192字节。 | | MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,需小于8192字节。 |
...@@ -33,25 +33,33 @@ getStorageSync(path: string): Storage ...@@ -33,25 +33,33 @@ getStorageSync(path: string): Storage
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- |
| [Storage](#storage) | 获取到要操作的Storage实例,用于进行数据存储操作。 |
**示例:** | 类型 | 说明 |
```js | ------------------- | ------------------------------------------------- |
import data_storage from '@ohos.data.storage' | [Storage](#storage) | 获取到要操作的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 ## data_storage.getStorage
...@@ -63,25 +71,33 @@ getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void ...@@ -63,25 +71,33 @@ getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ---------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
import data_storage from '@ohos.data.storage'
let path = '/data/storage/el2/database' ```js
data_storage.getStorage(path + '/mystore', function (err, storage) { 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) { if (err) {
console.info("Get the storage failed, path: " + path + '/mystore') console.info("Failed to get the storage. path: " + path + '/mystore');
return; return;
} }
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto');
storage.flushSync() storage.flushSync();
}) })
``` ```
## data_storage.getStorage ## data_storage.getStorage
...@@ -93,29 +109,37 @@ getStorage(path: string): Promise&lt;Storage&gt; ...@@ -93,29 +109,37 @@ getStorage(path: string): Promise&lt;Storage&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;[Storage](#storage)&gt; | Promise实例,用于异步获取结果。 |
**示例:** | 类型 | 说明 |
```js | ---------------------------------- | ------------------------------- |
import data_storage from '@ohos.data.storage' | Promise&lt;[Storage](#storage)&gt; | Promise实例,用于异步获取结果。 |
let path = '/data/storage/el2/database' **示例:**
let getPromise = data_storage.getStorage(path + '/mystore') ```js
getPromise.then((storage) => { import featureAbility from '@ohos.ability.featureAbility';
storage.putSync('startup', 'auto')
storage.flushSync() var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Get the storage failed, path: " + path + '/mystore') 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 ## data_storage.deleteStorageSync
...@@ -127,15 +151,25 @@ deleteStorageSync(path: string): void ...@@ -127,15 +151,25 @@ deleteStorageSync(path: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**示例:** **示例:**
```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
...@@ -147,22 +181,32 @@ deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -147,22 +181,32 @@ deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.deleteStorage(path + '/mystore', function (err) { 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) { if (err) {
console.info("Deleted failed with err: " + err) console.info("Failed to delete the storage with err: " + err);
return return;
} }
console.info("Deleted successfully.") console.info("Succeeded in deleting the storage.");
}) })
``` ```
## data_storage.deleteStorage ## data_storage.deleteStorage
...@@ -174,25 +218,36 @@ deleteStorage(path: string): Promise&lt;void&gt; ...@@ -174,25 +218,36 @@ deleteStorage(path: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 | | ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```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
...@@ -204,15 +259,24 @@ removeStorageFromCacheSync(path: string): void ...@@ -204,15 +259,24 @@ removeStorageFromCacheSync(path: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 | | path | string | 是 | 应用程序内部数据存储路径。 |
**示例:** **示例:**
```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
...@@ -224,22 +288,32 @@ removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -224,22 +288,32 @@ removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.removeStorageFromCache(path + '/mystore', function (err) { 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) { if (err) {
console.info("Removed storage from cache failed with err: " + err) console.info("Failed to remove storage from cache with err: " + err);
return return;
} }
console.info("Removed storage from cache successfully.") console.info("Succeeded in removing storage from cache.");
}) })
``` ```
## data_storage.removeStorageFromCache ## data_storage.removeStorageFromCache
...@@ -251,25 +325,36 @@ removeStorageFromCache(path: string): Promise&lt;void&gt; ...@@ -251,25 +325,36 @@ removeStorageFromCache(path: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 | | ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```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
...@@ -286,21 +371,24 @@ getSync(key: string, defValue: ValueType): ValueType ...@@ -286,21 +371,24 @@ getSync(key: string, defValue: ValueType): ValueType
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| defValue | [ValueType](#valuetype) | 是 | 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| defValue | [ValueType](#valuetype) | 是 | 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| ValueType | 键对应的值,如果值为null或者非默认值类型,返回默认数据。 | | --------- | -------------------------------------------------------- |
| ValueType | 键对应的值,如果值为null或者非默认值类型,返回默认数据。 |
**示例:** **示例:**
```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
...@@ -312,22 +400,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): ...@@ -312,22 +400,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;):
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ------------------------------ | ---- | ----------------------------------------- |
| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;ValueType&gt; | 是 | 回调函数。 | | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
| callback | AsyncCallback&lt;ValueType&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.get('startup', 'default', function(err, value) { ```js
storage.get('startup', 'default', function(err, value) {
if (err) { if (err) {
console.info("Get the value of startup failed with err: " + err) console.info("Failed to get the value of startup with err: " + err);
return return;
} }
console.info("The value of startup is " + value) console.info("The value of startup is " + value);
}) })
``` ```
### get ### get
...@@ -341,24 +431,26 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt; ...@@ -341,24 +431,26 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | ----------------------- | ---- | ----------------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;ValueType&gt; | Promise实例,用于异步获取结果。 | | ------------------------ | ------------------------------- |
| Promise&lt;ValueType&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```js
let promiseget = storage.get('startup', 'default') ```js
promiseget.then((value) => { let promiseget = storage.get('startup', 'default');
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
...@@ -370,15 +462,17 @@ putSync(key: string, value: ValueType): void ...@@ -370,15 +462,17 @@ putSync(key: string, value: ValueType): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | ------ | ----------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
**示例:** **示例:**
```js
storage.putSync('startup', 'auto') ```js
``` storage.putSync('startup', 'auto');
```
### put ### put
...@@ -390,22 +484,24 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void ...@@ -390,22 +484,24 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | -------- | ------------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.put('startup', 'auto', function (err) { ```js
storage.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup with err: " + err);
return return;
} }
console.info("Put the value of startup successfully.") console.info("Succeeded in putting the value of startup.");
}) })
``` ```
### put ### put
...@@ -417,25 +513,28 @@ put(key: string, value: ValueType): Promise&lt;void&gt; ...@@ -417,25 +513,28 @@ put(key: string, value: ValueType): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | ------ | ----------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promiseput = storage.put('startup', 'auto') ```js
promiseput.then(() => { let promiseput = storage.put('startup', 'auto');
console.info("Put the value of startup successfully.") promiseput.then(() => {
}).catch((err) => { console.info("Succeeded in putting the value of startup.");
console.info("Put the value of startup failed with err: " + err) }).catch((err) => {
}) console.info("Failed to put the value of startup with err: " + err);
``` })
```
### hasSync ### hasSync
...@@ -447,22 +546,25 @@ hasSync(key: string): boolean ...@@ -447,22 +546,25 @@ hasSync(key: string): boolean
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | ------ | ------ | ---- | ------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| boolean | true&nbsp;表示存在,false表示不存在。 | | ------- | ------------------------------------- |
| boolean | true&nbsp;表示存在,false表示不存在。 |
**示例:** **示例:**
```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
...@@ -474,28 +576,31 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean ...@@ -474,28 +576,31 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ---------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| boolean | true表示存在,false表示不存在。 | | ------- | ------------------------------- |
| boolean | true表示存在,false表示不存在。 |
**示例:** **示例:**
```js
storage.has('startup', function (err, isExist) { ```js
storage.has('startup', function (err, isExist) {
if (err) { if (err) {
console.info("Check the key of startup failed with err: " + err) console.info("Failed to check the key of startup with err: " + err);
return return;
} }
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") console.info("The key of startup is contained.");
} }
}) })
``` ```
### has ### has
...@@ -507,26 +612,29 @@ has(key: string): Promise&lt;boolean&gt; ...@@ -507,26 +612,29 @@ has(key: string): Promise&lt;boolean&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | ------ | ------ | ---- | ------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;boolean&gt; | Promise实例,用于异步处理。 | | ---------------------- | --------------------------- |
| Promise&lt;boolean&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promisehas = storage.has('startup') ```js
promisehas.then((isExist) => { let promisehas = storage.has('startup')
promisehas.then((isExist) => {
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") console.info("The key of startup is contained.");
} }
}).catch((err) => { }).catch((err) => {
console.info("Check the key of startup failed with err: " + err) console.info("Failed to check the key of startup with err: " + err);
}) })
``` ```
### deleteSync ### deleteSync
...@@ -538,14 +646,16 @@ deleteSync(key: string): void ...@@ -538,14 +646,16 @@ deleteSync(key: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称。它不能为空。 | | ------ | ------ | ---- | --------------------------------- |
| key | string | 是 | 要获取的存储key名称。它不能为空。 |
**示例:** **示例:**
```js
storage.deleteSync('startup') ```js
``` storage.deleteSync('startup');
```
### delete ### delete
...@@ -557,21 +667,23 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -557,21 +667,23 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.delete('startup', function (err) { ```js
storage.delete('startup', function (err) {
if (err) { if (err) {
console.info("Delete startup key failed with err: " + err) console.info("Failed to delete startup key failed err: " + err);
return return;
} }
console.info("Deleted startup key successfully.") console.info("Succeeded in deleting startup key.");
}) })
``` ```
### delete ### delete
...@@ -583,24 +695,27 @@ delete(key: string): Promise&lt;void&gt; ...@@ -583,24 +695,27 @@ delete(key: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称。 | | ------ | ------ | ---- | --------------------- |
| key | string | 是 | 要获取的存储key名称。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```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
...@@ -612,9 +727,10 @@ flushSync(): void ...@@ -612,9 +727,10 @@ flushSync(): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**示例:** **示例:**
```js
storage.flushSync() ```js
``` storage.flushSync();
```
### flush ### flush
...@@ -626,20 +742,22 @@ flush(callback: AsyncCallback&lt;void&gt;): void ...@@ -626,20 +742,22 @@ flush(callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.flush(function (err) { ```js
storage.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush to file with err: " + err);
return return;
} }
console.info("Flushed to file successfully.") console.info("Succeeded in flushing to file.");
}) })
``` ```
### flush ### flush
...@@ -651,19 +769,21 @@ flush(): Promise&lt;void&gt; ...@@ -651,19 +769,21 @@ flush(): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```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
...@@ -675,9 +795,10 @@ clearSync(): void ...@@ -675,9 +795,10 @@ clearSync(): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**示例:** **示例:**
```js
storage.clearSync() ```js
``` storage.clearSync();
```
### clear ### clear
...@@ -689,20 +810,22 @@ clear(callback: AsyncCallback&lt;void&gt;): void ...@@ -689,20 +810,22 @@ clear(callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.clear(function (err) { ```js
storage.clear(function (err) {
if (err) { if (err) {
console.info("Clear to file failed with err: " + err) console.info("Failed to clear the storage with err: " + err);
return return;
} }
console.info("Cleared to file successfully.") console.info("Succeeded in clearing the storage.");
}) })
``` ```
### clear ### clear
...@@ -714,19 +837,20 @@ clear(): Promise&lt;void&gt; ...@@ -714,19 +837,20 @@ clear(): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```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')
...@@ -738,20 +862,22 @@ on(type: 'change', callback: Callback&lt;StorageObserver&gt;): void ...@@ -738,20 +862,22 @@ on(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- | | 参数名 | 类型 | 说明 |
| type | string | 事件类型,固定值'change',表示数据变更。 | | -------- | --------------------------------------------------- | ---------------------------------------- |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 回调对象实例。 | | type | string | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 回调对象实例。 |
**示例:** **示例:**
```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')
...@@ -763,26 +889,28 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void ...@@ -763,26 +889,28 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- | | 参数名 | 类型 | 说明 |
| type | string | 事件类型,固定值'change',表示数据变更。 | | -------- | --------------------------------------------------- | ---------------------------------------- |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 需要取消的回调对象实例。 | | type | string | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 需要取消的回调对象实例。 |
**示例:** **示例:**
```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
**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
| 名称 | 参数类型 | 必填 | 说明 | | 名称 | 参数类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | ---- | -------- | ---- | ---------------- |
| key | string | 否 | 变更的数据内容。 | | key | string | 否 | 变更的数据内容。 |
## ValueType ## ValueType
......
...@@ -1124,13 +1124,14 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>, callback: AsyncCallbac ...@@ -1124,13 +1124,14 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>, callback: AsyncCallbac
**示例:** **示例:**
```js ```js
var names = ["status", "navigation"]; // 此处以不显示导航栏、状态栏为例
var names = [];
windowClass.setSystemBarEnable(names, (err, data) => { windowClass.setSystemBarEnable(names, (err, data) => {
if (err.code) { if (err.code) {
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(err));
return; return;
} }
console.info('Succeeded in setting the system bar to be visible. Data: ' + JSON.stringify(data)); console.info('Succeeded in setting the system bar to be invisible. Data: ' + JSON.stringify(data));
}); });
``` ```
...@@ -1157,12 +1158,13 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>): Promise&lt;void&gt; ...@@ -1157,12 +1158,13 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>): Promise&lt;void&gt;
**示例:** **示例:**
```js ```js
var names = ["status", "navigation"]; // 此处以不显示导航栏、状态栏为例
var names = [];
let promise = windowClass.setSystemBarEnable(names); let promise = windowClass.setSystemBarEnable(names);
promise.then((data)=> { promise.then((data)=> {
console.info('Succeeded in setting the system bar to be visible. Data: ' + JSON.stringify(data)); console.info('Succeeded in setting the system bar to be invisible. Data: ' + JSON.stringify(data));
}).catch((err)=>{ }).catch((err)=>{
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(err));
}); });
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册