• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Uniform Data Structs
2
3
4## When to Use
5
6The Unified Data Management Framework (UDMF) provides uniform data structs for some uniform data types, for example the widget type (**openharmony.form**) used on the home screen.
7
8Applications can directly use the uniform data structs in certain scenarios. For example, in the drag-and-drop operation across applications, you can write the data (encapsulated in a uniform struct) to be dragged to [DragEvent](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#dragevent). The target application (application requesting the data) reads the data from **DragEvent** and parses the data in the uniform data struct. Using uniform data structs for data interaction between applications effectively reduces the development workload in your app experience.
9
10## Uniform Data Structs
11
12The following table lists the uniform data structs provided by the UDMF.
13
14| Data Type               |       Data Struct       | Description      |
15| ----------------------- | :-------------------: | ---------- |
16| 'general.text'          |         Text          | Text      |
17| 'general.plain-text'    |       PlainText       | Plain text    |
18| 'general.hyperlink'     |       Hyperlink       | Hyperlink    |
19| 'general.html'          |         HTML          | HyperText Markup Language (HTML)    |
20| 'general.file'          |         File          | File      |
21| 'general.image'         |         Image         | Image      |
22| 'general.video'         |         Video         | Video      |
23| 'general.audio'         |         Audio         | Audio      |
24| 'general.folder'        |        Folder         | Folder    |
25| 'openharmony.form'      |   SystemDefinedForm   | Widget      |
26| 'openharmony.app-item'  | SystemDefinedAppItem  | Icon      |
27| 'openharmony.pixel-map' | SystemDefinedPixelMap | Pixel map|
28
29## Constraints
30
31The UDMF supports management of batch data records. Each data set cannot exceed 200 MB. The maximum size of a single attribute in **PlainText**, **Hyperlink**, or **HTML** is 20 MB.
32
33## Available APIs
34
35The UDMF provides a **UnifiedData** object to encapsulate multiple **UnifiedRecord**s. **UnifiedRecord** is an abstract definition of data supported by the UDMF, for example, a text or an image. The data records in a **UnifiedRecord** object are of the **UniformDataType** type.
36
37The following table lists the commonly used APIs for managing uniform data structs. For more information, see [Unified Data Channel](../reference/apis-arkdata/js-apis-data-unifiedDataChannel.md) and [Uniform Type Descriptor](../reference/apis-arkdata/js-apis-data-uniformTypeDescriptor.md).
38
39| Class       | API                              | Description                                                        |
40| ------------- | -------------------------------------- | ------------------------------------------------------------ |
41| UnifiedRecord | getType(): string                      | Obtains the type of this data record.                        |
42| UnifiedData   | constructor(record: UnifiedRecord)     | A constructor used to create a **UnifiedData** object with a data record.                    |
43| UnifiedData   | addRecord(record: UnifiedRecord): void | Adds a data record to this **UnifiedRecord** object.                      |
44| UnifiedData   | getRecords(): Array\<UnifiedRecord>    | Obtains all data records from this **UnifiedData** object. The data obtained is of the **UnifiedRecord** type. Before using the data, you need to obtain the data type by using **getType** and convert the data type to a child class.|
45
46
47## How to Develop
48
49The following describes how to encapsulate an image and a plaint text into an object in a uniform data struct and manage the data.
50
511. Import the **@ohos.data.unifiedDataChannel** and **@ohos.data.uniformTypeDescriptor** modules.
52
53   ```ts
54   import unifiedDataChannel from '@ohos.data.unifiedDataChannel';
55   import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor';
56   ```
572. Create an image object and create a **UnifiedData** instance with the image object.
58
59   - Create an image object.
60
61      ```ts
62      let image = new unifiedDataChannel.Image();
63      ```
64
65   - Modify the image object attributes.
66
67      ```ts
68      // The image object has the imageUri attribute.
69      image.imageUri = '...';
70      ```
71
72   - Access the image object attribute.
73
74      ```ts
75      console.info(`imageUri = ${image.imageUri}`);
76      ```
77
78   - Create a **UnifiedData** instance with the image object.
79
80      ```ts
81      let unifiedData = new unifiedDataChannel.UnifiedData(image);
82      ```
833. Create a plain text data record and add it to the **UnifiedData** instance created.
84
85   ```ts
86   let plainText = new unifiedDataChannel.PlainText();
87   plainText.textContent = 'this is textContent of plainText';
88   plainText.abstract = 'abstract of plainText';
89   plainText.details = {
90     plainKey1: 'plainValue1',
91     plainKey2: 'plainValue2',
92   };
93   unifiedData.addRecord(plainText);
94   ```
954. Obtain all data records in this **UnifiedData** instance.
96
97   ```ts
98   let records = unifiedData.getRecords();
99   ```
1005. Traverse each record, determine the data type, and convert the data type into a child class object to obtain the original data.
101
102   ```ts
103   for (let i = 0; i < records.length; i ++) {
104     // Obtain the type of each data record.
105     let type = records[i].getType();
106     switch (type) {
107       case uniformTypeDescriptor.UniformDataType.IMAGE:
108         // Convert the data type to obtain the image.
109         let image = records[i] as unifiedDataChannel.Image;
110         break;
111       case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
112         // Convert the data to obtain the plain text.
113         let plainText = records[i] as unifiedDataChannel.PlainText;
114         break;
115       default:
116         break;
117     }
118   }
119   ```
120