当前位置:网站首页>Distributed data object: HyperTerminal 'global variable'

Distributed data object: HyperTerminal 'global variable'

2022-06-10 04:18:00 Openharmony developer community

3 Released at the end of the month OpenAtom OpenHarmony( hereinafter referred to as “OpenHarmony”)3.1 Release In the version , New distributed data object feature . What is a distributed data object ? In this issue, let's take a look at ~

One 、 Background introduction

OpenHarmony As a distributed operating system , Support running on different terminal devices . These terminal devices through cross end migration and multi end collaboration , It can provide rich services for users 、 Smooth full scene experience . In this distributed scenario , Data exchange is required between different devices to realize the cooperation between devices .
Let's take a look at the following example of a distributed scenario :
Example : Touch screen and TV Cooperation
stay TV/PAD When watching video on , Control... On a touchscreen device TV/PAD Play status on 、 speed of progress 、 Volume and speed, etc , Realize multi terminal control .
chart 1 Multi terminal control , Liberate equipment restrictions
 
To implement the functions of the above example , First, data synchronization between devices must be realized . In the traditional way , Data synchronization between devices , The developer needs to complete the message processing logic , Include : Establish communication links 、 Messaging processing 、 Error retry 、 Data conflict resolution and other operations , Very heavy workload . And the more equipment , Debugging complexity will grow exponentially . Is there a simpler way to implement ? Through the analysis of , We found that , The playback status in the example 、 speed of progress 、 Volume and speed are actually variables . If these variables support “ overall situation ” visit , Then developers can access these variables across devices as if they were local variables , Data can be automatically and efficiently 、 Conveniently realize multi terminal synchronization .
So , This time OpenHarmony v3.1 Release The distributed data object feature has been added in version . Distributed data objects provide developers with easy-to-use tools in distributed application scenarios JS Interface , Easily realize data collaboration between multiple devices and the same application , At the same time, the equipment can monitor the status and data changes of the object . Compared with the traditional way , Distributed data objects greatly reduce the workload of developers .
( For ease of description , Later “ Distributed data objects ” It's also called “ object ”.)
 

Two 、 Principle analysis

How to implement such a simple and efficient distributed data object technology ? Let's analyze them one by one ~
 
1. Object data synchronization
Distributed data objects , The most important function is data synchronization between objects . Devices in trusted networks can create distributed data objects locally , And set up sessionID. Distributed data objects on different devices , By setting the same sessionID, Establish synchronization between objects .
Pictured 2 Shown , equipment A And equipment B Upper “ Distributed data objects 1”, Its sessionID Are all session1, These two objects establish session1 The synchronization relationship of .
chart 2 Synchronization relationship of objects
 
In a synchronous relationship , Only one object can be added to a device . For example 3 in , equipment A Of “ Distributed data objects 1” Has joined session1 The synchronization relationship of , therefore , equipment A Of “ Distributed data objects 2” Just failed to join .
After the synchronization relationship is established , Every session There is a shared object data . Joined the same session The object of , The following operations are supported :
(1) Read / modify session Data in .
(2) Monitor data changes , Perceive the modification of shared object data by other objects .
(3) Monitoring status changes , Perceive the addition and departure of other objects .
About data synchronization of distributed data objects , It is worth noting that , The smallest unit of synchronization is “ attribute ”. such as , chart 4 Middle target 1 There are three properties :name、age and parents. When one of the properties changes , Only the changed attributes need to be synchronized during data synchronization .
chart 3 Data view
 
2. Object lifecycle
Next , Let's take a comprehensive look at distributed data objects from the perspective of life cycle .
chart 4 Life cycle
 
Pictured 5 Shown , Objects include three states : uninitialized 、 Local and distributed objects . These three states are described as follows :
chart 5 Three states of an object
 
Depending on the conditions , Objects switch back and forth between these three states :
In the beginning , The object is in an uninitialized state . After instantiation , The object switches from the uninitialized state to the local object state . Set object sessionID, After receiving the online notification of the peer device object , At this point, data can be synchronized across devices , The object enters the distributed object state .
Local or remote equipment offline , perhaps sessionID Be cleared of , Then the object switches back to the local object state . Instances of distributed objects and processes are stored in memory , When the application exits , Distributed objects and in memory databases are also destroyed , Object goes directly into uninitialized state .
 

3、 ... and 、 Development constraints and cases

After the introduction above , We have a full understanding of distributed data objects , Here's what developers are most concerned about “ How to use it? ” The problem. .
Before using distributed data objects , Let's first explain the relevant development constraints :
1. A single application can only create at most 16 Distributed data object instances .
2. Considering performance and user experience , Not more than 3 Data collaboration with multiple devices .
3. Considering performance and user experience , The size of distributed data objects is limited to 500KB within .
4. The data synchronization of distributed data objects takes place in the same application , Same with session ID Between .
Next , We use a simple development case to explain how to use distributed data objects . In this case , equipment A And equipment B Create one containing 3 Property object , These two objects are added to the same session, Establish synchronization relationship . The property changes of one object will be automatically synchronized to another object , So as to achieve “ Global variables ” effect .
Through this case , We can master the basic operation of distributed data objects , Include :
1. Create objects
2. Set up sessionID
3. Set the callback for listening object changes
4. Monitoring status changes
 
The code example is as follows :
1. equipment A Of JS Code example :
 
import distributedObject from'@ohos.data.distributedDataObject'// Create objects , Object contains three properties :name、age and isVisvar g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false}); // Set up sessionID by “123456” g_object.setSessionId("123456"); // Set the callback for listening object changes changeCallback : function (sessionId, changeData) { if (changeData != null && changeData != undefined) { changeData.forEach(element => { console.info("changed !" + element + " " + g_object[element]); }); } } g_object.on("change", this.changeCallback);
 
2. equipment B Of JS Code example :
 
// Create objects , Object contains three properties :name、age and isVisvar g_object = distributedObject.createDistributedObject({name:"Amy", age:undefined, isVis:false}); // Set up sessionID by “123456”, This session There is already equipment in the A The object of g_object.setSessionId("123456"); // Monitoring status changes statusCallback : function (sessionId, networkid, status) { // Successfully joined session And the device is detected A go online if (status == "online" && networkid == networkid_A) { // Device at this time A Of age The value is 18, And local age The value is undefined, adopt console.info Automatic synchronization device A Of age Data to local . If you want to use local data , You can put age The initial value is changed to a valid value , image name equally . console.info ("age = {g_object.age}"); g_object.name = "jack"; // Device at this time A Of changeCallback Receive callback for object change , Print out “changed !name jack”.// Use later g_object. All attribute changes will be automatically synchronized to the device A, meanwhile g_object. Property to access data yes session The latest data in ( Also includes equipment A The change on ). equipment A and B It is equivalent to using the same global variable g_object. } } g_object.on("status", this.statusCallback);
 
In this issue , We explained the background of distributed data objects 、 Principle and development case . If you want to experience more detailed distributed data objects , Welcome to join OpenHarmony Open source project , The project corresponds to the address of the distributed database warehouse :
https://gitee.com/openharmony/distributeddatamgr_objectstore
The subsequent warehouses will also send out distributed data objects one after another sample Oh , Coming soon !
 
原网站

版权声明
本文为[Openharmony developer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100416465756.html