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

!19204 翻译完成:18599+18504+18877+18953 几个API少量刷新

Merge pull request !19204 from wusongqing/zh20230605-3.2release
......@@ -4,7 +4,7 @@ The **AbilityDelegatorRegistry** module provides APIs for storing the global reg
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityDelegatorRegistry](js-apis-app-ability-abilityDelegatorRegistry.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
......
......@@ -4,7 +4,7 @@ The **AbilityManager** module provides APIs for obtaining, adding, and modifying
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityManager](js-apis-app-ability-abilityManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are system APIs and cannot be called by third-party applications.
## Modules to Import
......
......@@ -4,7 +4,7 @@ The **missionManager** module provides APIs to lock, unlock, and clear missions,
> **NOTE**
>
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.missionManager](js-apis-app-ability-missionManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
......
......@@ -40,6 +40,8 @@ Creates the context based on the bundle name.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name | Type | Mandatory | Description |
......@@ -102,6 +104,8 @@ Creates the context based on the bundle name and module name.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name | Type | Mandatory | Description |
......
......@@ -77,7 +77,6 @@ let media = mediaLibrary.getMediaLibrary();
### getFileAssets<sup>7+</sup>
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains file assets (also called files). This API uses an asynchronous callback to return the result.
......@@ -105,7 +104,7 @@ async function example() {
selectionArgs: [imageType.toString()],
};
// Obtain the files in asynchronous callback mode.
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
media.getFileAssets(imagesFetchOp, async (error, fetchFileResult) => {
// Check whether the result set of the obtained files is undefined. If yes, the API call fails.
if (fetchFileResult == undefined) {
console.error('get fetchFileResult failed with error: ' + error);
......@@ -124,8 +123,8 @@ async function example() {
return;
}
console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in asynchronous callback mode.
fetchFileResult.getFirstObject((error, fileAsset) => {
// Obtain the first file in the result set in asynchronous callback mode. If there are a large number of files, use getAllObject instead.
fetchFileResult.getFirstObject(async (error, fileAsset) => {
// Check whether the first file is undefined. If yes, the API call fails.
if (fileAsset == undefined) {
console.error('get first object failed with error: ' + error);
......@@ -178,7 +177,7 @@ async function example() {
selectionArgs: [imageType.toString()],
};
// Obtain the files in promise mode.
media.getFileAssets(imagesFetchOp).then((fetchFileResult) => {
media.getFileAssets(imagesFetchOp).then(async (fetchFileResult) => {
// Obtain the total number of files in the result set.
const count = fetchFileResult.getCount();
// Check whether the number is less than 0. If yes, the API call fails.
......@@ -192,8 +191,8 @@ async function example() {
return;
}
console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in promise mode.
fetchFileResult.getFirstObject().then((fileAsset) => {
// Obtain the first file in the result set in promise mode. If there are a large number of files, use getAllObject instead.
fetchFileResult.getFirstObject().then(async (fileAsset) => {
console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
// Call getNextObject to obtain the next file until the last one.
for (let i = 1; i < count; i++) {
......@@ -514,9 +513,10 @@ Obtains the albums. This API uses an asynchronous callback to return the result.
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
if (albumList != undefined) {
......@@ -554,9 +554,10 @@ Obtains the albums. This API uses a promise to return the result.
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
console.info('getAlbums successfully: ' + JSON.stringify(albumList));
......@@ -2021,7 +2022,7 @@ async function example() {
### getNextObject<sup>7+</sup>
getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result.
> **NOTE**
......@@ -2049,7 +2050,8 @@ async function example() {
};
let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject();
if (!fileAsset.isAfterLast) {
console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject((error, fileAsset) => {
if (error) {
console.error('fetchFileResult getNextObject failed with error: ' + error);
......@@ -2065,7 +2067,7 @@ async function example() {
### getNextObject<sup>7+</sup>
getNextObject(): Promise&lt;FileAsset&gt;
getNextObject(): Promise&lt;FileAsset&gt;
Obtains the next file asset in the result set. This API uses a promise to return the result.
> **NOTE**
......@@ -2093,7 +2095,8 @@ async function example() {
};
let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject();
if (!fileAsset.isAfterLast) {
console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject().then((fileAsset) => {
console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close();
......@@ -2369,9 +2372,10 @@ Commits the modification in the album attributes to the database. This API uses
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
......@@ -2406,9 +2410,10 @@ Commits the modification in the album attributes to the database. This API uses
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
......@@ -2423,6 +2428,47 @@ async function example() {
### getFileAssets<sup>7+</sup>
getFileAssets(callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains the file assets in this album. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.READ_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback used to return the file assets.|
**Example**
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
// Obtain the albums that meet the retrieval options and return the album list.
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
// Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album.
album.getFileAssets((error, fetchFileResult) => {
if (error) {
console.error('album getFileAssets failed with error: ' + error);
return;
}
let count = fetchFileResult.getCount();
console.info('album getFileAssets successfully, count: ' + count);
fetchFileResult.close();
});
}
```
### getFileAssets<sup>7+</sup>
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains the file assets in this album. This API uses an asynchronous callback to return the result.
......@@ -2442,9 +2488,10 @@ Obtains the file assets in this album. This API uses an asynchronous callback to
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
let fileNoArgsfetchOp = {
selections: '',
......@@ -2492,9 +2539,10 @@ Obtains the file assets in this album. This API uses a promise to return the res
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
let fileNoArgsfetchOp = {
selections: '',
......
# @ohos.taskpool (Using the Task Pool)
# @ohos.taskpool (Starting the Task Pool)
The task pool provides a multi-thread running environment for applications. It helps reduce resource consumption and improve system performance. It also frees you from caring about the lifecycle of thread instances. You can use the **TaskPool** APIs to create background tasks and perform operations on them, for example, executing or canceling a task. Theoretically, you can create an unlimited number of tasks, but this is not recommended for memory considerations. In addition, you are not advised performing blocking operations in a task, especially indefinite blocking. Long-time blocking operations occupy worker threads and may block other task scheduling, adversely affecting your application performance.
......@@ -60,12 +60,12 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
function func(args) {
console.log("func: " + args);
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
let task = new taskpool.Task(func, "this is my first Task");
let task = new taskpool.Task(printArgs, "this is my first Task");
```
### Attributes
......@@ -112,17 +112,17 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
function func(args) {
console.log("func: " + args);
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
let value = await taskpool.execute(func, 100);
async function taskpoolExecute() {
let value = await taskpool.execute(printArgs, 100);
console.log("taskpool result: " + value);
}
taskpoolTest();
taskpoolExecute();
```
## taskpool.execute
......@@ -136,7 +136,7 @@ Places a task in the internal task queue of the task pool. The task will be dist
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------------- |
| -------- | --------------------- | ---- | ------------------------------------ |
| task | [Task](#task) | Yes | Task to be executed. |
| priority | [Priority](#priority) | No | Priority of the task (not supported yet).|
......@@ -160,18 +160,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts
@Concurrent
function func(args) {
console.log("func: " + args);
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
let task = new taskpool.Task(func, 100);
async function taskpoolExecute() {
let task = new taskpool.Task(printArgs, 100);
let value = await taskpool.execute(task);
console.log("taskpool result: " + value);
}
taskpoolTest();
taskpoolExecute();
```
## taskpool.cancel
......@@ -200,14 +200,14 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example of successful task cancellation**
```ts
function func(args) {
"use concurrent";
console.log("func: " + args);
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
let task = new taskpool.Task(func, 100);
async function taskpoolCancel() {
let task = new taskpool.Task(printArgs, 100);
taskpool.execute(task);
try {
taskpool.cancel(task);
......@@ -216,20 +216,20 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
**Example of a failure to cancel a task that has been executed**
```ts
function func(args) {
"use concurrent";
console.log("func: " + args);
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
let task = new taskpool.Task(func, 100);
async function taskpoolCancel() {
let task = new taskpool.Task(printArgs, 100);
let value = taskpool.execute(task);
let start = new Date().getTime();
while (new Date().getTime() - start < 1000) {// Wait for 1s to ensure that the task has been executed.
......@@ -243,25 +243,25 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
**Example of a failure to cancel an ongoing task**
```ts
function func(args) {
"use concurrent";
console.log("func: " + args);
@Concurrent
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
let task1 = new taskpool.Task(func, 100);
let task2 = new taskpool.Task(func, 200);
let task3 = new taskpool.Task(func, 300);
let task4 = new taskpool.Task(func, 400);
let task5 = new taskpool.Task(func, 500);
let task6 = new taskpool.Task(func, 600);
async function taskpoolCancel() {
let task1 = new taskpool.Task(printArgs, 100);
let task2 = new taskpool.Task(printArgs, 200);
let task3 = new taskpool.Task(printArgs, 300);
let task4 = new taskpool.Task(printArgs, 400);
let task5 = new taskpool.Task(printArgs, 500);
let task6 = new taskpool.Task(printArgs, 600);
let res1 = taskpool.execute(task1);
let res2 = taskpool.execute(task2);
......@@ -276,7 +276,7 @@ async function taskpoolTest() {
}
}
taskpoolTest();
taskpoolCancel();
```
## Additional Information
......@@ -288,7 +288,7 @@ The following sequenceable data types are supported: All Primitive Type (excludi
- The task pool APIs can be used only in the module with **compileMode** set to **esmodule** in the stage model. To check the **compileMode** setting of a module, open the **build-profile.json5** file of the module and check for **"compileMode": "esmodule"** under **buildOption**.
- A task in the task pool can reference only variables passed in by input parameters or imported variables, rather than closure variables. The decorator **@Concurrent** is used to intercept unsupported variables.
- A task in the task pool supports only common functions or async functions, rather than class member functions or anonymous functions. The decorator **@Concurrent** is used to intercept unsupported functions.
- The decorator **@Concurrent** can be used only in the .ets file. To create a task in the task pool in the .ts file, use the statement **use concurrent**.
- The decorator **@Concurrent** can be used only in .ets files.
### Using the Task Pool in Simple Mode
......@@ -297,14 +297,14 @@ The following sequenceable data types are supported: All Primitive Type (excludi
```ts
// Common functions are supported, and variables passed in by input parameters are also supported.
@Concurrent
function func(args) {
console.log("func: " + args);
function printArgs(args) {
console.log("printArgs: " + args);
return args;
}
async function taskpoolTest() {
async function taskpoolExecute() {
// taskpool.execute(task)
let task = new taskpool.Task(func, "create task, then execute");
let task = new taskpool.Task(printArgs, "create task, then execute");
let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1);
......@@ -313,7 +313,7 @@ async function taskpoolTest() {
console.log("taskpool.execute(function) result: " + val2);
}
taskpoolTest();
taskpoolExecute();
```
**Example 2**
......@@ -328,24 +328,24 @@ export var c = 2000;
import { c } from "./b";
@Concurrent
function test(a) {
function printArgs(a) {
console.log(a);
console.log(c);
return a;
}
async function taskpoolTest() {
async function taskpoolExecute() {
// taskpool.execute(task)
let task = new taskpool.Task(test, "create task, then execute");
let task = new taskpool.Task(printArgs, "create task, then execute");
let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1);
// taskpool.execute(function)
let val2 = await taskpool.execute(test, "execute task by func");
let val2 = await taskpool.execute(printArgs, "execute task by func");
console.log("taskpool.execute(function) result: " + val2);
}
taskpoolTest();
taskpoolExecute();
```
**Example 3**
......@@ -353,57 +353,52 @@ taskpoolTest();
```ts
// The async functions are supported.
@Concurrent
async function task() {
async function delayExcute() {
let ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]);
return ret;
}
async function taskpoolTest() {
taskpool.execute(task).then((result) => {
async function taskpoolExecute() {
taskpool.execute(delayExcute).then((result) => {
console.log("TaskPoolTest task result: " + result);
});
}
taskpoolTest();
taskpoolExecute();
```
**Example 4**
```ts
// Use use concurrent to create a task in the task pool in the .ts file.
// c.ts
function test1(n) {
"use concurrent"
return n;
// c.ets
@Concurrent
function strSort(inPutArr) {
let newArr = inPutArr.sort();
return newArr;
}
export async function taskpoolTest1() {
console.log("taskpoolTest1 start");
var task = new taskpool.Task(test1, 100);
export async function func1() {
console.log("taskpoolTest start");
let strArray = ['c test string', 'b test string', 'a test string'];
var task = new taskpool.Task(strSort, strArray);
var result = await taskpool.execute(task);
console.log("taskpoolTest1 result:" + result);
console.log("func1 result:" + result);
}
async function test2() {
"use concurrent"
var ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]);
return ret;
}
export async function taskpoolTest2() {
export async function func2() {
console.log("taskpoolTest2 start");
taskpool.execute(test2).then((result) => {
console.log("TaskPoolTest2 result: " + result);
let strArray = ['c test string', 'b test string', 'a test string'];
taskpool.execute(strSort, strArray).then((result) => {
console.log("func2 result: " + result);
});
}
```
```ts
/ / a.ets (in the same directory as c.ts)
/ / a.ets (in the same directory as c.ets)
import { taskpoolTest1, taskpoolTest2 } from "./c";
taskpoolTest1();
taskpoolTest2();
func1();
func2();
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册