自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony—分布式數(shù)據(jù)對象之功能實(shí)踐

系統(tǒng) OpenHarmony
通過本文的學(xué)習(xí)可了解到分布式數(shù)據(jù)對象的主要接口、大致調(diào)用方法和功能實(shí)現(xiàn)等。結(jié)合對應(yīng)內(nèi)容,可以進(jìn)一步對分布式數(shù)據(jù)對象同步機(jī)制做深度學(xué)習(xí)。

??想了解更多內(nèi)容,請?jiān)L問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://ost.51cto.com??

分布式數(shù)據(jù)對象之功能實(shí)踐

分布式數(shù)據(jù)對象管理框架是一款面向?qū)ο蟮膬?nèi)存數(shù)據(jù)管理框架,向應(yīng)用開發(fā)者提供內(nèi)存對象的創(chuàng)建、查詢、刪除、修改、訂閱等基本數(shù)據(jù)對象的管理能力,同時具備分布式能力,滿足超級終端場景下,相同應(yīng)用多設(shè)備間的數(shù)據(jù)對象協(xié)同需求。

1、 基本概念

分布式數(shù)據(jù)對象提供JS接口,讓開發(fā)者能以使用本地對象的方式使用分布式對象。

2、 運(yùn)作示意圖

3、約束與限制

  • 不同設(shè)備間只有相同bundleName的應(yīng)用才能直接同步。
  • 不建議創(chuàng)建過多分布式對象,每個分布式對象將占用100-150KB內(nèi)存。
  • 每個對象大小不超過500KB。
  • 支持JS接口間的互通,與其他語言不互通。

4、開發(fā)指導(dǎo)

(1) 接口說明

引用分布式對象頭文件:

import distributedObject from '@ohos.data.distributedDataObject'

接口:

DistrubutedObject:

5、 開發(fā)步驟

(1) 引入接口

import distributedObject from '@ohos.data.distributedDataObject'

(2) 創(chuàng)建對象

// 創(chuàng)建對象,對象包含3個基本屬性:name,age和isVis;2個復(fù)雜屬性:parent,list
var g_object = distributedObject.createDistributedObject({name:undefined, age:undefined, isVis:true, parent:undefined, list:undefined});

說明:**構(gòu)造分布式對象時,新增了默認(rèn)SESSION_ID屬性,并為各屬性增加了set和get方法,其構(gòu)造方法如下所示:

const SESSION_ID = "__sessionId";
class Distributed {
constructor(obj) {
this.__proxy = obj;
Object.keys(obj).forEach(key => {
Object.defineProperty(this, key, {
enumerable: true,
configurable: true,
get: function () {
return this.__proxy[key];
},
set: function (newValue) {
this.__proxy[key] = newValue;
}
});
});
Object.defineProperty(this, SESSION_ID, {
enumerable: true,
configurable: true,
get: function () {
return this.__proxy[SESSION_ID];
},
set: function (newValue) {
this.__proxy[SESSION_ID] = newValue;
}
});
this.__objectId = randomNum();
console.info("constructor success ");
}

//------------------------------------------------------其余內(nèi)容此處省略

__proxy;
__objectId;
}

(3) 加入同步組網(wǎng)

發(fā)起方:

用distributedObject.genSessionId()方法生成隨機(jī)字符串,并設(shè)置為對象的__sessionId屬性的值。

g_object.setSessionId(distributedObject.genSessionId());

說明:**setSessionId不僅設(shè)置了SESSION_ID屬性的值,也重新打包了對象,增加了該SESSION_ID對應(yīng)同步組網(wǎng)的內(nèi)容,具體方法如下:

setSessionId(sessionId) {
if (sessionId == null || sessionId == "") {
leaveSession(this.__proxy); //退出同步組網(wǎng)
return false;
}
if (this.__proxy[SESSION_ID] == sessionId) {
console.info("same session has joined " + sessionId);
return true;
}
leaveSession(this.__proxy); //退出同步組網(wǎng)
//加入sessionId值對應(yīng)的同步組網(wǎng),返回新生成的對象
let object = joinSession(this.__proxy, this.__objectId, sessionId);
if (object != null) {
this.__proxy = object;//替換新生成的對象
return true;
}
return false;
}

被拉起方:

發(fā)起方將sessionId通過Intent傳到對端設(shè)備,被拉起方獲取Intent中的sessionId,執(zhí)行setSessionId加入同步組網(wǎng)完成數(shù)據(jù)同步。

//sessionId與發(fā)起方的__sessionId一致
g_object.setSessionId(sessionId);

(4) 監(jiān)聽對象變更

開啟change監(jiān)聽,當(dāng)同步組網(wǎng)內(nèi)對象屬性value發(fā)生變化時,觸發(fā)用戶自定義回調(diào)changeCallback。

changeCallback : function (sessionId,  changeData) {
console.info("change" + sessionId + " " + this.response);
if (changeData != null && changeData != undefined) {
changeData.forEach(element => {
console.info("changed !" + element + " " + g_object[element]);
});
}
}
g_object.on("change", this.changeCallback);

(5)修改對象屬性

g_object.name = "jack";
g_object.age = 19;
g_object.isVis = false;
g_object.parent = {mother:"jack mom",father:"jack Dad"};
g_object.list = [{mother:"jack mom"}, {father:"jack Dad"}];
// 對端設(shè)備收到change回調(diào),fields為name,age,isVis,parent和list

說明: 針對復(fù)雜類型的數(shù)據(jù)修改,目前支持對根屬性的修改,暫不支持對下級屬性的修改。示例如下:

//支持的修改方式
g_object.parent = {mother:"mom", father:"dad"};
//不支持的修改方式
g_object.parent.mother = "mom";

(6) 訪問對象

console.info("name " + g_object["name"]); //訪問到的是組網(wǎng)內(nèi)最新數(shù)據(jù)

(7)刪除監(jiān)聽數(shù)據(jù)變更

//刪除變更回調(diào)changeCallback
g_object.off("change", changeCallback);
//刪除所有的變更回調(diào)
g_object.off("change");

(8) 監(jiān)聽分布式對象的上下線

開啟status監(jiān)聽,當(dāng)同步組網(wǎng)內(nèi)有對象在線狀態(tài)發(fā)生變化時,觸發(fā)用戶自定義回調(diào)statusCallback。

statusCallback : function (sessionId, networkid, status) {
this.response += "status changed " + sessionId + " " + status + " " + networkId;
}
g_object.on("status", this.changeCallback);

(9) 刪除監(jiān)聽分布式對象的上下線

//刪除上下線回調(diào)changeCallback
g_object.off("status", changeCallback);
//刪除所有的上下線回調(diào)
g_object.off("status");

(10)退出同步組網(wǎng)

//兩種方式均可
g_object.setSessionId("");
g_object.setSessionId();

6 、內(nèi)部實(shí)現(xiàn)

(1) 主要接口

class DistributedObjectStoreImpl : public DistributedObjectStore
{
public:
DistributedObject *CreateObject(const std::string &sessionId) override;
uint32_t DeleteObject(const std::string &sessionId) override;
//此處的watch主要針對change監(jiān)聽
uint32_t Watch(DistributedObject *object, std::shared_ptr<ObjectWatcher> watcher) override;
uint32_t UnWatch(DistributedObject *object) override;
//其余內(nèi)容省略
}

(2)調(diào)用順序

7 、總結(jié)

通過本文的學(xué)習(xí)可了解到分布式數(shù)據(jù)對象的主要接口、大致調(diào)用方法和功能實(shí)現(xiàn)等。結(jié)合對應(yīng)內(nèi)容,可以進(jìn)一步對分布式數(shù)據(jù)對象同步機(jī)制做深度學(xué)習(xí)。

下一步:

(1) 著重理解同步組網(wǎng)的構(gòu)建原理。

(2) 著重理解同步組網(wǎng)內(nèi),監(jiān)聽change和status變化的實(shí)現(xiàn)。

??想了解更多內(nèi)容,請?jiān)L問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://ost.51cto.com??

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2023-02-20 15:38:38

2023-02-21 16:41:41

分布式相機(jī)鴻蒙

2023-02-20 15:29:14

分布式相機(jī)鴻蒙

2022-07-27 14:30:15

分布式數(shù)據(jù)鴻蒙

2022-07-18 10:29:33

數(shù)據(jù)分布式系統(tǒng)

2022-02-17 18:08:04

OpenHarmon應(yīng)用開發(fā)鴻蒙

2021-12-14 10:16:00

鴻蒙HarmonyOS應(yīng)用

2022-06-15 16:16:21

分布式數(shù)據(jù)庫鴻蒙

2023-02-28 07:01:11

分布式緩存平臺

2009-06-12 11:42:28

EJB分布式

2022-06-20 15:32:55

Stage模型分布式開發(fā)

2022-04-24 16:00:03

Ability鴻蒙

2022-09-07 08:18:26

分布式灰度方案分支號

2021-10-30 19:30:23

分布式Celery隊(duì)列

2024-01-05 07:28:50

分布式事務(wù)框架

2022-03-21 19:44:30

CitusPostgreSQ執(zhí)行器

2021-11-10 16:10:18

鴻蒙HarmonyOS應(yīng)用

2013-03-22 14:44:52

大規(guī)模分布式系統(tǒng)飛天開放平臺

2016-01-12 14:59:40

分布式存儲分布式存儲架構(gòu)

2022-12-28 09:48:09

分布式系統(tǒng)關(guān)鍵路徑
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號