• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 合理使用系统提供的接口
2<!--Kit: Common-->
3<!--Subsystem: Demo&Sample-->
4<!--Owner: @mgy917-->
5<!--Designer: @jiangwensai-->
6<!--Tester: @Lyuxin-->
7<!--Adviser: @huipeizi-->
8
9## 简介
10
11在应用开发中,经常会调用系统提供的接口,比如读取本地文件、处理服务端数据等等。若对接口使用不合理,可能引起延迟、卡顿、丢帧等性能问题。本文以如下系统提供的接口为例,总结了使用中的注意事项。
12
13## wordBreak属性
14
15零宽空格(Zero Width Space, ZWSP)是一个特殊的Unicode字符。它是一个不可见的字符,其宽度为零,不占用任何可见空间。在文本处理系统中,尽管它在视觉上是不可见的,但它在文本中确实存在,并可以作为潜在的断点,即允许在此位置断开行。这意味着如果一行文本过长需要自动换行时,文本可以在零宽空格的位置进行折行,而不影响单词的完整性。
16
17虽然零宽空格在许多情况下都是有用的,但它也可能引起问题,特别是在文本处理和数据清洗中。不注意这些看不见的字符可能导致数据的意外错误、搜索失败、数据不一致等问题。因此,在处理来自不同源的文本数据时,了解和考虑这些不可见字符是非常重要的。
18
19避免在文本组件内使用零宽空格(\u200b)的形式来设置断行规则,推荐使用[wordBreak](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md#wordbreak11),wordBreak在使用性能方面优于零宽空格。例如推荐用法为:Text(this.diskName).wordBreak(WordBreak.BREAK_ALL)。
20
21### 反例
22
23```ts
24@CustomDialog
25export struct DiskFormatDialog {
26  private diskName: string = '';
27  private customDialogController: CustomDialogController;
28
29  build() {
30    Column() {
31      Text(this.diskName.split("").join("\u200B"))
32      .textAlign(TextAlign.Start)
33    }
34  }
35}
36```
37
38通过[SmartPerf Host](performance-optimization-using-smartperf-host.md)工具抓取Trace。启动时ReceiveVsync阶段耗时为3s271ms。
39
40![](figures/reasonable_using_system_interfaces_2.PNG)
41
42### 正例
43
44```ts
45@CustomDialog
46export struct DiskFormatDialog {
47  private diskName: string = '';
48  private customDialogController: CustomDialogController;
49
50  build() {
51    Column() {
52      Text(this.diskName)
53      .textAlign(TextAlign.Start)
54      .wordBreak(WordBreak.BREAK_ALL)
55    }
56  }
57}
58```
59
60通过[SmartPerf Host](performance-optimization-using-smartperf-host.md)工具抓取Trace。启动时ReceiveVsync阶段耗时为301ms。
61
62![](figures/reasonable_using_system_interfaces_4.PNG)
63
64### 总结
65
66使用零宽空格时(3s271ms)比使用wordBreak时(301ms)耗时更多。所以当需要使用类似方法时,使用wordBreak性能更优。
67