DEV Community

SameX
SameX

Posted on

The Art of Data Synchronization in HarmonyOS Next Part Two: In-depth Understanding of Standardized Data Types (UTD)

This article aims to conduct an in-depth exploration of the technical details of Huawei's HarmonyOS Next system (up to API 12 currently), summarizing based on actual development practices. Mainly serving as a vehicle for technical sharing and communication, it is inevitable that there may be errors and omissions. Colleagues are welcomed to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprinting must indicate the source and the original author.

Design Principles of UTD

  • Hierarchical Structure: UTD classifies types using a hierarchical structure, which is convenient for developers to understand and use. For example, the image type can be classified under the media type, and the picture type can be further classified under the image type.
  • Physical and Logical Classification: UTD divides data types into physical and logical categories, enabling developers to describe data types from different dimensions. For example, the picture type can be both an image object and a file.
  • Predefined and Customizable: UTD provides predefined data types such as pictures, videos, and audio, which are convenient for developers to use quickly. Meanwhile, developers can also customize data types to meet personalized needs. #### Advantages of UTD
  • Type Ambiguity Issue: UTD solves the problem of type ambiguity in the HarmonyOS system, such as inconsistent descriptions of the same type of data by different applications.
  • Cross-application Compatibility: UTD improves the compatibility between different applications, making it easier for applications to share and exchange data.
  • Development Efficiency: UTD reduces the workload of developers in defining and parsing data types, thus improving development efficiency. #### Usage of UTD
  • Querying UTD Information: Developers can use the interfaces provided by ArkData to query UTD information, such as querying the UTD type based on the file extension or the MIME type.
  • Creating UTD Objects: Developers can use the interfaces provided by ArkData to create UTD objects, such as creating picture UTD objects, video UTD objects, etc.
  • Properties of UTD Objects: UTD objects contain a series of properties such as "uniformDataType", "filenameExtensions", "mimeTypes", etc. Developers can access and modify these properties as needed. Example Code:
// Query the file type
let fileType = uniformTypeDescriptor.getUniformDataTypeByFilenameExtension('.jpg');
console.log(fileType.typeId); // Output: general.image
// Create a picture UTD object
let imageUTD = new uniformTypeDescriptor.ImageUTD({
  uniformDataType: 'general.image',
  filenameExtensions: ['.jpg', '.png', '.gif'],
  mimeTypes: ['image/jpeg', 'image/png', 'image/gif'],
});
// Get the properties of the picture UTD object
console.log(imageUTD.filenameExtensions); // Output: ['.jpg', '.png', '.gif']
console.log(imageUTD.mimeTypes); // Output: ['image/jpeg', 'image/png', 'image/gif']
Enter fullscreen mode Exit fullscreen mode

In-depth Understanding of Standardized Data Structures

Advantages of Standardized Data Structures

  • Uniform Data Format: Standardized data structures define a uniform format for data content, facilitating data exchange and sharing between different applications.
  • Simplified Data Parsing: Standardized data structures simplify the complexity of data parsing, making it easier for developers to process data.
  • Improved Development Efficiency: Standardized data structures reduce the workload of developers in defining and parsing data formats, thus improving development efficiency. #### Usage of Standardized Data Structures
  • Creating Standardized Data Structures: Developers can use the interfaces provided by ArkData to create standardized data structures, such as creating picture data structures, video data structures, etc.
  • Properties of Standardized Data Structures: Standardized data structures contain a series of properties such as the width and height of pictures, resolution, URL, etc. Developers can access and modify these properties as needed.
  • Applications of Standardized Data Structures: Standardized data structures can be applied to various scenarios such as file preview, file sharing, and dragging. Example Code:
// Create a picture data structure
let imageStruct = new uniformDataStruct.ImageStruct({
  uniformDataType: 'general.image',
  width: 1920,
  height: 1080,
  url: 'https://www.example.com/image.jpg',
});
// Get the properties of the picture data structure
console.log(imageStruct.width); // Output: 1920
console.log(imageStruct.height); // Output: 1080
console.log(imageStruct.url); // Output: https://www.example.com/image.jpg
Enter fullscreen mode Exit fullscreen mode

Summary

UDMF is a powerful data management framework in Huawei's HarmonyOS Next. It achieves the unification and standardization of data interaction between different applications and different devices through standardized data types and standardized data structures. We can easily achieve cross-application and cross-device data interaction using the UDMF module and build secure and reliable applications.

Top comments (0)