提交 f7d3d376 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 9f08600c
......@@ -90,9 +90,9 @@ Use the following APIs to delete a **Storage** instance or data file.
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
let promise = dataStorage.getStorage(path + '/mystore');
});
```
......@@ -120,14 +120,15 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
promise.then((storage) => {
let getPromise = storage.get('startup', 'default');
let getPromise = storage.get('startup', 'default')
getPromise.then((value) => {
console.info("The value of startup is " + value);
}).catch((err) => {
console.info("Failed to get the value of startup with err: " + err);
})
}).catch((err) => {
console.info("Failed to get the storage.");
console.info("Failed to get the storage.")
})
```
......@@ -149,7 +150,7 @@ Use the following APIs to delete a **Storage** instance or data file.
var observer = function (key) {
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.flushSync(); // Trigger the StorageObserver callback.
......@@ -169,7 +170,7 @@ Use the following APIs to delete a **Storage** instance or data file.
promise.then(() => {
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Failed to deleted the storage with err: " + err);
console.info("Failed to delete the storage with err: " + err);
})
```
......@@ -3,10 +3,13 @@
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**
> **NOTE**<br/>
>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> - The APIs of this module are no longer maintained since API Version 9. You are advised to use [`@ohos.data.preferences`](js-apis-data-preferences.md).
>
> - The APIs of this module can be used only in the FA model.
## Modules to Import
......@@ -35,15 +38,15 @@ Reads the specified file and loads its data to the **Storage** instance for data
**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. |
| ------------------- | ------------------------------------------------- |
| [Storage](#storage) | **Storage** instance used for data storage operations.|
**Example**
......@@ -55,11 +58,11 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
let storage = data_storage.getStorageSync(path + '/mystore');
storage.putSync('startup', 'auto');
storage.flushSync();
let storage = data_storage.getStorageSync(path + '/mystore');
storage.putSync('startup', 'auto');
storage.flushSync();
});
```
......@@ -73,9 +76,9 @@ Reads the specified file and loads its data to the **Storage** instance for data
**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.|
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes | Callback used to return the execution result. |
**Example**
......@@ -88,16 +91,16 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
data_storage.getStorage(path + '/mystore', function (err, storage) {
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();
})
})
});
```
......@@ -111,15 +114,15 @@ Reads the specified file and loads its data to the **Storage** instance for data
**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. |
| ---------------------------------- | ------------------------------- |
| Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|
**Example**
......@@ -131,15 +134,15 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
let getPromise = data_storage.getStorage(path + '/mystore');
getPromise.then((storage) => {
let getPromise = data_storage.getStorage(path + '/mystore');
getPromise.then((storage) => {
storage.putSync('startup', 'auto');
storage.flushSync();
}).catch((err) => {
}).catch((err) => {
console.info("Failed to get the storage. path: " + path + '/mystore');
})
})
});
```
......@@ -153,9 +156,9 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**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**
......@@ -167,12 +170,11 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
data_storage.deleteStorageSync(path + '/mystore');
data_storage.deleteStorageSync(path + '/mystore');
});
```
## data_storage.deleteStorage
deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
......@@ -183,9 +185,9 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**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.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -198,15 +200,15 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
data_storage.deleteStorage(path + '/mystore', function (err) {
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.");
})
})
});
```
......@@ -220,13 +222,14 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**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. |
**Example**
......@@ -239,14 +242,14 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
let promisedelSt = data_storage.deleteStorage(path + '/mystore');
promisedelSt.then(() => {
let promisedelSt = data_storage.deleteStorage(path + '/mystore');
promisedelSt.then(() => {
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
}).catch((err) => {
console.info("Failed to delete the storage with err: " + err);
})
})
});
```
......@@ -259,10 +262,9 @@ 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**
......@@ -274,9 +276,9 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
data_storage.removeStorageFromCacheSync(path + '/mystore');
data_storage.removeStorageFromCacheSync(path + '/mystore');
});
```
......@@ -290,9 +292,9 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**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.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -305,15 +307,15 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
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.");
})
})
});
```
......@@ -327,14 +329,14 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**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. |
**Example**
......@@ -347,22 +349,20 @@ var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromise====================>");
});
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
console.info("Succeeded in removing storage from cache.");
}).catch((err) => {
}).catch((err) => {
console.info("Failed to remove storage from cache with err: " + err);
})
})
});
```
## Storage
Provides APIs for obtaining and modifying storage data.
### getSync
getSync(key: string, defValue: ValueType): ValueType
......@@ -373,16 +373,16 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**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. |
| 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. |
| --------- | -------------------------------------------------------- |
| 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**
......@@ -402,10 +402,10 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**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. |
| 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**
......@@ -431,18 +431,19 @@ Obtains the value corresponding to a key. If the value is null or not in the def
**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. |
| 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. |
| ------------------------ | ------------------------------- |
| Promise&lt;ValueType&gt; | Promise used to return the result.|
**Example**
```js
let promiseget = storage.get('startup', 'default');
promiseget.then((value) => {
......@@ -463,15 +464,15 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**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. |
| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value.|
**Example**
```js
storage.putSync('startup', 'auto')
storage.putSync('startup', 'auto');
```
......@@ -485,10 +486,10 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**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. |
| 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**
......@@ -514,18 +515,19 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**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. |
| 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. |
**Example**
```js
let promiseput = storage.put('startup', 'auto');
promiseput.then(() => {
......@@ -546,15 +548,15 @@ Checks whether the storage object contains data with a given key.
**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. |
| ------- | ------------------------------------- |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
**Example**
......@@ -576,16 +578,16 @@ Checks whether the storage object contains data with a given key. This API uses
**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.|
| 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. |
| ------- | ------------------------------- |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
**Example**
......@@ -612,15 +614,15 @@ Checks whether the storage object contains data with a given key. This API uses
**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. |
| ---------------------- | --------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result.|
**Example**
......@@ -646,14 +648,14 @@ Deletes data with the specified key from this storage object.
**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')
storage.deleteSync('startup');
```
......@@ -667,9 +669,9 @@ Deletes data with the specified key from this storage object. This API uses an a
**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.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -695,14 +697,14 @@ Deletes data with the specified key from this storage object. This API uses a pr
**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. |
**Example**
......@@ -728,7 +730,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Example**
```js
storage.flushSync()
storage.flushSync();
```
......@@ -742,8 +744,8 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -770,7 +772,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Return value**
| Type | Description |
| ------------------- | ------------------------------ |
| ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise that returns no value. |
**Example**
......@@ -796,7 +798,7 @@ Clears this **Storage** object.
**Example**
```js
storage.clearSync()
storage.clearSync();
```
......@@ -810,8 +812,8 @@ Clears this **Storage** object. This API uses an asynchronous callback to return
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------------------------- | --------- | ------------------------------- |
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -836,9 +838,8 @@ 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. |
**Example**
......@@ -864,8 +865,8 @@ Subscribes to data changes. The **StorageObserver** needs to be implemented. Whe
**Parameters**
| Name | Type | Description |
| -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| type | string | Event type. The value **change** indicates data change events. |
| -------- | --------------------------------------------------- | ---------------------------------------- |
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes. |
**Example**
......@@ -891,8 +892,8 @@ Unsubscribes from data changes.
**Parameters**
| Name | Type | Description |
| -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| type | string | Event type. The value **change** indicates data change events. |
| -------- | --------------------------------------------------- | ---------------------------------------- |
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes. |
**Example**
......@@ -909,9 +910,9 @@ storage.off('change', observer);
**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
......@@ -920,7 +921,7 @@ 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. |
\ No newline at end of file
| boolean | The value is of Boolean type.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册