• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Unified Data Definition
2
3
4## When to Use
5
6To streamline cross-application data interaction of OpenHarmony and minimize the application/service data interaction costs, the Unified Data Management Framework (UDMF) provides standard data definitions to define common data types. Applications can use the APIs provided by the UDMF to create and use these data types.
7
8For example, in the cross-application drag scenario, the application of the drag source writes the data to be dragged to a [drag event](../reference/arkui-ts/ts-universal-events-drag-drop.md#dragevent) based on the standard data definitions. The application of the drop target reads the dragged data from the drag event and parses the data based on the standard data definitions. The data dragged between different applications complies with the same standard definitions, which avoids exhaustive data type adaptation and effectively reduces the development workload.
9
10## Unified Data Types
11
12The UDMF provides the following unified data types:
13
14**Basic data types**
15
16Basic data types include File and Text, which can be used for cross-application and cross-platform data interaction. Figure 1 and Figure 2 illustrate the basic data types.
17
18**Figure 1** UDMF File
19
20![UDMF_FILE](figures/udmf_type_File.png)
21
22**Figure 2** UDMF Text
23
24![UDMF_TEXT](figures/udmf_type_Text.png)
25
26**System Defined Types (SDTs)**
27
28The SDTs are specific to the platform or operating system, such as Form (UI card information), AppItem (app description information), and PixelMap (thumbnail). This type of data can be used for cross-application data interaction in a system or platform. Figure 3 illustrates the SDT data.
29
30**Figure 3** UDMF SDT data
31
32![UDMF_SDT](figures/udmf_type_SDT.png)
33
34**App Defined Type (ADT)**
35
36The SDT data is application-specific. This type of data can be used for across-platform data interaction for an application. As shown in Figure 4, the MyFile file format can be defined for use in an application ecosystem.
37
38**Figure 4** UDMF ADT data
39
40![UDMF_ADT](figures/udmf_type_ADT.png)
41
42## Restrictions
43
44- The size of each data record in the UDMF cannot exceed 2 MB.
45- The UDMF supports data group management. The size of each group cannot exceed 4 MB.
46
47## Available APIs
48
49The UDMF provides the unified data object **UnifiedData** to encapsulate a group of data records **UnifiedRecord**. **UnifiedRecord** is an abstract definition of data content supported by the UDMF, for example, a text record or an image record. The data content type in a data record corresponds to **UniformDataType**.
50
51The following table describes common UDMF APIs. For more information about the APIs, see [Unified Data Channel](../reference/apis/js-apis-data-unifiedDataChannel.md) and [Standard Data Definition and Description](../reference/apis/js-apis-data-uniformTypeDescriptor.md).
52
53| Class          | API          | Description                                                                                           |
54|---------------|-------------------|-----------------------------------------------------------------------------------------------|
55| UnifiedRecord | getType(): string                      | Obtains the data type of this data record.|
56| UnifiedData   | constructor(record: UnifiedRecord)     | A constructor used to create a **UnifiedData** object with a data record.                                                    |
57| UnifiedData   | addRecord(record: UnifiedRecord): void | Adds a data record to this **UnifiedRecord** object.                                                     |
58| UnifiedData   | getRecords(): Array\<UnifiedRecord> | Obtains all data records from this **UnifiedData** object. The data obtained is of the **UnifiedRecord** type. You need to obtain the data type by using **getType** and convert the data type to a child class before using it.|
59
60
61## How to Develop
62
63The following describes how to create a **UnifiedData** object containing two data records: image and plain text.
64
651. Import the **@ohos.data.unifiedDataChannel** and **@ohos.data.uniformTypeDescriptor** modules.
66
67   ```ts
68   import unifiedDataChannel from '@ohos.data.unifiedDataChannel';
69   import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor';
70   ```
71
722. Create an image data record and initialize the **UnifiedData** object with the image data record.
73
74    (1) Create an image data record.
75
76   ```ts
77   let image = new unifiedDataChannel.Image();
78   ```
79
80    (2) Modify object attributes.
81
82   ```ts
83   // The Image object contains the imageUri attribute.
84   image.imageUri = '...';
85   ```
86
87    (3) Access the object attributes.
88
89   ```ts
90   console.info(`imageUri = ${image.imageUri}`);
91   ```
92
93    (4) Create a **UnifiedData** instance.
94
95   ```ts
96   let unifiedData = new unifiedDataChannel.UnifiedData(image);
97   ```
98
993. Create a plain text data record and add it to the **UnifiedData** instance created.
100
101   ```ts
102   let plainText = new unifiedDataChannel.PlainText();
103   plainText.textContent = 'this is textContent of plainText';
104   plainText.abstract = 'abstract of plainText';
105   plainText.details = {
106     plainKey1: 'plainValue1',
107     plainKey2: 'plainValue2',
108   };
109   unifiedData.addRecord(plainText);
110   ```
111
1124. Obtain all data records in this **UnifiedData** instance.
113
114   ```ts
115   let records = unifiedData.getRecords();
116   ```
117
1185. Traverse each record, determine the data type of the record, and convert the record into a child class object to obtain the original data record.
119
120   ```ts
121   for (let i = 0; i < records.length; i ++) {
122     // Read the type of the data record.
123     let type = records[i].getType();
124     switch (type) {
125       case uniformTypeDescriptor.UniformDataType.IMAGE:
126         // Convert the data to obtain the original image data record.
127         let image = records[i] as unifiedDataChannel.Image;
128         break;
129       case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
130         // Convert the data to obtain the original text record.
131         let plainText = records[i] as unifiedDataChannel.PlainText;
132         break;
133       default:
134         break;
135     }
136   }
137   ```
138