DEV Community

SameX
SameX

Posted on

The Art of Data Synchronization in HarmonyOS Next Part One: Unveiling ArkData Management

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.
This article will introduce the core data management framework in Huawei's HarmonyOS Next - ArkData Management (ArkData), and discuss its role and importance in the HarmonyOS system.

What is ArkData Management?

ArkData Management (ArkData) is an important component in Huawei's HarmonyOS Next. It provides developers with a unified mechanism for data storage, management, and synchronization. ArkData aims to solve the problem of data interaction between different applications and different devices, and enhance the efficiency and security performance of data management.

The Role and Importance of ArkData Management

  • Uniform Data Management: ArkData provides a set of unified definitions of data types and structures, such as Standardized Data Types (UTD) and standardized data structures, used to describe different types of data and standardize the format of data interaction.
  • Data Storage and Persistence: ArkData supports multiple data storage methods, including user preferences, key-value databases, relational databases, etc. Developers can choose the appropriate storage solution according to the characteristics of the data.
  • Data Synchronization: ArkData supports cross-device data synchronization functions, such as distributed data objects and relational databases. Developers can utilize these functions to synchronize application data between different devices.
  • Data Security and Reliability: ArkData provides a series of data security and reliability mechanisms, such as data backup, data recovery, data encryption, etc., to ensure the security and reliability of data. ## How to Integrate the ArkData Module in a Project To use the ArkData module in a project, you need to follow these steps:
  • Import the Module: In your project, you need to import the relevant classes and interfaces of the ArkData module.
  • Create Data Objects: According to your needs, choose an appropriate storage method to create data objects, such as creating a key-value database, a relational database, or user preferences.
  • Operate on Data: Use the interfaces provided by the ArkData module to perform operations such as adding, deleting, modifying, and querying on the data objects.
  • Data Synchronization: If cross-device data synchronization is required, configure the synchronization parameters of distributed data objects or relational databases, and call the corresponding interfaces to perform data synchronization. Example Code:
// Import the ArkData module
import { distributedKVStore } from '@kit.ArkData';
// Create a key-value database
let kvManager = distributedKVStore.createKVManager(kvManagerConfig);
let kvStore = kvManager.getKVStore('storeId', options);
// Insert data
kvStore.put('key1', 'value1');
// Query data
let value = kvStore.get('key1');
console.log(value); // Output: value1
Enter fullscreen mode Exit fullscreen mode

Standardized Data Types (UTD)

  • The Role of UTD: UTD defines information such as the identification, ownership relationship, and brief description of different types of data, such as pictures, videos, audio, etc. It can help applications identify and filter different types of data, such as for file preview and file sharing.
  • The Advantages of UTD: The introduction of UTD makes data interaction between different applications and different devices more standardized and consistent, reducing development costs and enhancing the user experience.
  • The Use of UTD: Developers can use the interfaces provided by ArkData to query and obtain UTD information, such as uniformTypeDescriptor.getUniformDataTypeByFilenameExtension and uniformTypeDescriptor.getTypeDescriptor. Example Code:
// Query the file type
let fileType = uniformTypeDescriptor.getUniformDataTypeByFilenameExtension('.jpg');
console.log(fileType.typeId); // Output: general.image
// Get the details of the file type
let fileDetails = uniformTypeDescriptor.getTypeDescriptor(fileType.typeId);
console.log(fileDetails.description); // Output: Picture
Enter fullscreen mode Exit fullscreen mode

Standardized Data Structures

  • The Definition of Standardized Data Structures: Standardized data structures define a unified data content structure for some UTD data types, such as the width, height, and resolution information of pictures, as well as the URL and description of hyperlinks.
  • The Role of Standardized Data Structures: Standardized data structures can facilitate cross-application and cross-device data interaction, such as dragging and dropping.
  • The Use of Standardized Data Structures: Developers can use the interfaces provided by ArkData to create and operate on standardized data structures, such as uniformDataStruct.Hyperlink and uniformDataStruct.PlainText. Example Code:
// Create a hyperlink
let hyperlink = new uniformDataStruct.Hyperlink({
  uniformDataType: 'general.hyperlink',
  url: 'https://www.example.com',
  description: 'Example Hyperlink',
});
// Create plain text
let plainText = new uniformDataStruct.PlainText({
  uniformDataType: 'general.plain-text',
  textContent: 'Example Text',
});
// Create a unified data object
let unifiedData = new unifiedDataChannel.UnifiedData();
unifiedData.addRecord(new unifiedDataChannel.UnifiedRecord(hyperlink));
unifiedData.addRecord(new unifiedDataChannel.UnifiedRecord(plainText));
// Print the content of the unified data object
console.log(unifiedData.getRecords());
Enter fullscreen mode Exit fullscreen mode

Distributed Data Objects

  • The Definition of Distributed Data Objects: Distributed data objects are a memory object synchronization mechanism in Huawei's HarmonyOS Next. They can synchronize the data of an object between multiple devices.
  • The Role of Distributed Data Objects: Distributed data objects can be used in scenarios such as cross-device migration and multi-end collaboration, such as in game applications and multi-person collaborative applications.
  • The Use of Distributed Data Objects: Developers can use the interfaces provided by ArkData to create and operate on distributed data objects, such as distributedDataObject.create and distributedDataObject.setSessionId. Example Code:
// Create a distributed data object
let dataObject = distributedDataObject.create(context, data);
// Set the synchronization session ID
dataObject.setSessionId(sessionId);
// Listen for data changes
dataObject.on('change', (sessionId, fields) => {
  console.log(`Data change: ${fields.join(',')}`);
});
// Change the data
dataObject.title = 'New Title';
Enter fullscreen mode Exit fullscreen mode

Relational Database (RelationalStore)

  • The Definition of Relational Database: The relational database is a persistent storage mechanism in Huawei's HarmonyOS Next. It is based on the SQLite component and supports complex database operations such as adding, deleting, modifying, querying, transactions, and indexing.
  • The Role of Relational Database: The relational database can be used to store complex data relationships, such as contacts and calendars.
  • The Use of Relational Database: Developers can use the interfaces provided by ArkData to create and operate on relational databases, such as relationalStore.getRdbStore and relationalStore.executeSql. Example Code:
// Create a relational database
let store = relationalStore.getRdbStore(context, storeConfig);
// Create a data table
store.executeSql('CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER)');
// Insert data
store.insert('EMPLOYEE', { name: 'Alice', age: 30 });
// Query data
let resultSet = store.query('EMPLOYEE', ['ID', 'NAME', 'AGE']);
while (resultSet.goToNextRow()) {
  console.log(`ID: ${resultSet.getInt('ID')}, NAME: ${resultSet.getString('NAME')}, AGE: ${resultSet.getInt('AGE')}`);
}
resultSet.close();
Enter fullscreen mode Exit fullscreen mode

Key-Value Database (KV-Store)

  • The Definition of Key-Value Database: The key-value database is a lightweight persistent storage mechanism in Huawei's HarmonyOS Next. It stores data in the form of key-value pairs and supports fast read and write operations.
  • The Role of Key-Value Database: The key-value database can be used to store simple data, such as configuration information and user preference settings.
  • The Use of Key-Value Database: Developers can use the interfaces provided by ArkData to create and operate on key-value databases, such as distributedKVStore.createKVManager and distributedKVStore.getKVStore. Example Code:
// Create a key-value database
let kvManager = distributedKVStore.createKVManager(kvManagerConfig);
let kvStore = kvManager.getKVStore('storeId', options);
// Insert data
kvStore.put('key1', 'value1');
// Query data
let value = kvStore.get('key1');
console.log(value); // Output: value1
Enter fullscreen mode Exit fullscreen mode

User Preferences (Preferences)

  • The Definition of User Preferences: User preferences are a lightweight configuration data storage mechanism in Huawei's HarmonyOS Next. They store data in the form of key-value pairs and can listen for data changes.
  • The Role of User Preferences: User preferences can be used to store application configuration information and user preference settings.
  • The Use of User Preferences: Developers can use the interfaces provided by ArkData to create and operate on user preferences, such as preferences.getPreferencesSync and preferences.putSync. Example Code:
// Create user preferences
let preferences = preferences.getPreferencesSync(context, options);
// Write data
preferences.putSync('key1', 'value1');
// Read data
let value = preferences.getSync('key1');
console.log(value); // Output: value1
// Delete data
preferences.deleteSync('key1');
Enter fullscreen mode Exit fullscreen mode

Summary

ArkData Management (ArkData) is a powerful data management framework in Huawei's HarmonyOS Next. It provides us developers with convenient data storage, management, and synchronization functions, and enhances the efficiency and security performance of data management. Developers can easily achieve cross-application and cross-device data interaction using the ArkData module and build secure and reliable applications.

Top comments (0)