DEV Community

SameX
SameX

Posted on

The Art of Data Synchronization in HarmonyOS Next: Handling of Common Data Types

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a carrier for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprint must indicate the source and the original author.

Introduction

In Huawei HarmonyOS Next, the standardized data structure plays a crucial role. It provides us developers with a unified data - processing framework, making cross - application and cross - device data interaction simpler and more efficient. This article will conduct an in - depth exploration of common data types in the standardized data structure, including plain text, hyperlinks, HTML, etc., and elaborate on their attributes, applicable scenarios, and practical operation methods in applications.

Detailed Explanation of Common Data Types

Plain Text (Text)

The plain text data type is the most basic data form in applications. It does not contain any format information and is suitable for simple text information transmission.
Attribute Description:

  • text: The string content, representing plain text information. Applicable Scenarios:
  • Display of user - input information.
  • System message prompts.
  • Text content sharing. Creating the Plain Text Data Type:
import { UnifiedData, UnifiedRecord } from '@ohos.arkui';
// Create a plain text data object
let textData = new UnifiedData();
// Create a plain text record
let textRecord = new UnifiedRecord({
    type: 'text',
    value: {
        text: 'This is a plain text example.'
    }
});
// Add the record to the data object
textData.addRecord(textRecord);
Enter fullscreen mode Exit fullscreen mode

Hyperlink (Hyperlink)

The hyperlink data type is used to represent the address of network resources. It allows users to directly access web pages or other resources by clicking the link.
Attribute Description:

  • uri: The uniform resource identifier, pointing to the location of the network resource.
  • title: The descriptive title of the link. Applicable Scenarios:
  • Sharing of web page links.
  • Resource jumps between applications. Creating the Hyperlink Data Type:
// Create a hyperlink data object
let hyperlinkData = new UnifiedData();
// Create a hyperlink record
let hyperlinkRecord = new UnifiedRecord({
    type: 'hyperlink',
    value: {
        uri: 'https://www.example.com',
        title: 'Visit the example website'
    }
});
// Add the record to the data object
hyperlinkData.addRecord(hyperlinkRecord);
Enter fullscreen mode Exit fullscreen mode

HTML (HyperText Markup Language)

The HTML data type is used to store and transfer formatted text content. It supports rich text styles and layouts.
Attribute Description:

  • html: An HTML - formatted string, containing text content and style information.
  • baseUri: The base URL, used to resolve relative paths in HTML. Applicable Scenarios:
  • Display of web page content.
  • Rich - text editing and sharing. Creating the HTML Data Type:
// Create an HTML data object
let htmlData = new UnifiedData();
// Create an HTML record
let htmlRecord = new UnifiedRecord({
    type: 'html',
    value: {
        html: '<h1>This is an HTML title</h1><p>This is a paragraph.</p>',
        baseUri: 'https://www.example.com'
    }
});
// Add the record to the data object
htmlData.addRecord(htmlRecord);
Enter fullscreen mode Exit fullscreen mode

Operation and Processing of Data Types

In applications, developers not only need to create data types but also need to operate and process them. Here are some common operation examples:

Reading the Content of Data Types

// Read the plain text content
console.log(textRecord.getValue().text);
// Read the URI of the hyperlink
console.log(hyperlinkRecord.getValue().uri);
// Read the HTML content
console.log(htmlRecord.getValue().html);
Enter fullscreen mode Exit fullscreen mode

Updating the Content of Data Types

// Update the plain text content
textRecord.setValue({
    text: 'Updated plain text example.'
});
// Update the URI of the hyperlink
hyperlinkRecord.setValue({
    uri: 'https://www.newexample.com',
    title: 'Visit the new example website'
});
// Update the HTML content
htmlRecord.setValue({
    html: '<h2>This is the updated HTML title</h2><p>This is the updated paragraph.</p>',
    baseUri: 'https://www.newexample.com'
});
Enter fullscreen mode Exit fullscreen mode

Summary

The standardized data structure of Huawei HarmonyOS Next provides developers with a powerful set of tools for creating, operating, and processing various common data types. By understanding the attributes and applicable scenarios of these data types, developers can build applications more flexibly and achieve a rich variety of user interactions. This article, through detailed code examples, shows how to create and operate plain text, hyperlink, and HTML data types in applications, providing us developers with practical references and guidance.

Top comments (0)