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.
Lifecycle of Distributed Data Objects
The lifecycle of distributed data objects includes the following states:
- Uninitialized: Not instantiated or already destroyed.
- Local Data Object: The corresponding data table has been created, but data synchronization is not yet possible.
- Distributed Data Object: The corresponding data table has been created, the device is online, and the number of objects with the same SessionId within the network group is >= 2, allowing data to be synchronized across devices. If the device goes offline or the SessionId is set to null, the distributed data object reverts to a local data object. #### Synchronization Mechanism of Distributed Data Objects The synchronization mechanism of distributed data objects can be divided into the following steps:
- Create a Distributed Data Object: Developers use the
distributedDataObject.create
method to create an instance of a distributed data object and pass in the business data.- Set the Synchronization Session ID: Developers use the
dataObject.setSessionId
method to set the synchronization session ID to ensure that objects on different devices can synchronize data.- Data Mapping: The "read" or "assignment" operations on the distributed data object by the developer will be automatically mapped to the get and put operations of the in-memory database. Any change to each property will trigger a data synchronization operation.
- Data Synchronization: When the data of a distributed data object on one device changes, the system will automatically synchronize the changed data to other devices with the same SessionId. The synchronization process is carried out asynchronously, and the developer will be notified of the synchronization result through a callback function.
- Data Change Notification: The distributed data objects on other devices will also listen for data change events, and when they receive a data change notification, they will automatically update the local data. #### Efficiency and Performance of Data Synchronization The synchronization mechanism of distributed data objects adopts the following measures to improve efficiency and performance:
- Minimum Data Synchronization Unit: Data synchronization takes the property as the minimum unit. Only when a property changes will the data synchronization operation be triggered, reducing the amount of unnecessary synchronized data.
- Asynchronous Data Synchronization: Data synchronization is carried out asynchronously, avoiding blocking the running of the application program.
- Data Compression: The system will automatically compress the synchronized data to reduce the amount of data transmitted over the network.
- Data Verification: The system will verify the synchronized data to ensure the consistency and integrity of the data. ### Complete Code Example for Implementing the Synchronization of Distributed Data Objects Example Code:
// Import modules
import { distributedDataObject } from '@kit.ArkData';
// 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 data
dataObject.title = 'New title';
dataObject.text = 'New text';
// Add listening for status changes
dataObject.on('status', (sessionId, networkId, status) => {
console.log(`Status change: SessionId=${sessionId}, NetworkId=${networkId}, Status=${status}`);
});
// Add data persistence
dataObject.save(deviceId);
// Add data revocation
dataObject.revokeSave(callback);
// Add asset binding
dataObject.bindAssetStore(assetKey, bindInfo, callback);
Code Explanation:
-
Import Modules: First, import the
distributedDataObject
module, which provides interfaces for creating and operating on distributed data objects. -
Create a Distributed Data Object: Use the
distributedDataObject.create
method to create an instance of a distributed data object and pass in the business data. -
Set the Synchronization Session ID: Use the
dataObject.setSessionId
method to set the synchronization session ID to ensure that objects on different devices can synchronize data. -
Listen for Data Changes: Use the
dataObject.on
method to listen for data change events. When data changes occur, the callback function will be triggered. - Change Data: Trigger the data synchronization operation by modifying the properties of the distributed data object.
-
Add Listening for Status Changes: Use the
dataObject.on
method to listen for status change events, such as device connection status, data synchronization status, etc. -
Add Data Persistence: Use the
dataObject.save
method to persist the distributed data object on the device so that the data will not be lost even if the device is restarted. -
Add Data Revocation: Use the
dataObject.revokeSave
method to revoke the previously saved distributed data object and release device space. -
Add Asset Binding: Use the
dataObject.bindAssetStore
method to bind the distributed data object to assets, such as files, pictures, etc., to achieve cross-device synchronization of assets. ### Summary The synchronization of distributed data objects is a powerful feature in Huawei HarmonyOS Next. It allows us to easily synchronize application data between multiple devices. By thoroughly understanding concepts such as the distributed in-memory database, the cross-device data synchronization mechanism, the minimum unit of data synchronization, and the lifecycle, we can effectively utilize this feature to build multi-terminal collaborative applications.
Top comments (0)