We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

ArkUI Data Updates

When data, regardless of whether it is downloaded from the Internet or generated locally, needs to be sent to the UI thread for display, the annotations and the @Sendable decorator in ArkUI cannot simultaneously decorate variables and objects. Therefore, it is necessary to use makeObserved to import observable Sendable data into ArkUI.

This example describes the following scenarios:

  • When makeObserved is used with @Sendable data, it enables observability of changes that can trigger UI refreshes.
  • Data is fetched from a child thread and used to replace the observable data in the UI thread entirely.
  • The data fetched from the child thread is reprocessed with makeObserved to become observable.
  • Only non-observable data is passed from the UI main thread back to the child thread. The return value of makeObserved cannot be directly passed to child threads.
Collapse
Word wrap
Dark theme
Copy code
  1. // SendableData.ets
  2. @Sendable
  3. export class SendableData {
  4. name: string = 'Tom';
  5. age: number = 20;
  6. gender: number = 1;
  7. likes: number = 1;
  8. follow: boolean = false;
  9. }
Collapse
Word wrap
Dark theme
Copy code
  1. import { taskpool } from '@kit.ArkTS';
  2. import { SendableData } from './SendableData';
  3. import { UIUtils } from '@kit.ArkUI';
  4. @Concurrent
  5. function threadGetData(param: string): SendableData {
  6. // Process data in the child thread.
  7. let ret = new SendableData();
  8. console.info(`Concurrent threadGetData, param ${param}`);
  9. ret.name = param + "-o";
  10. ret.age = Math.floor(Math.random() * 40);
  11. ret.likes = Math.floor(Math.random() * 100);
  12. return ret;
  13. }
  14. @Entry
  15. @ComponentV2
  16. struct Index {
  17. // Use makeObserved to add observation to a regular object or a @Sendable object.
  18. @Local send: SendableData = UIUtils.makeObserved(new SendableData());
  19. build() {
  20. Column() {
  21. Text(this.send.name)
  22. Button("change name").onClick(() => {
  23. // Changes to properties are observable.
  24. this.send.name += "0";
  25. })
  26. Button("task").onClick(() => {
  27. // Enqueue the function to be executed in the task pool, waiting to be dispatched to a worker thread.
  28. // Data construction and processing can be done in the child thread, but observable data cannot be passed to the child thread (observable data can only be manipulated in the UI main thread).
  29. // Therefore, only the 'name' property of 'this.send' is passed to the child thread.
  30. taskpool.execute(threadGetData, this.send.name).then(val => {
  31. // Used together with @Local to observe changes to 'this.send'.
  32. this.send = UIUtils.makeObserved(val as SendableData);
  33. })
  34. })
  35. }
  36. }
  37. }
Search in Guides
Enter a keyword.