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

ActiveData在HarmonyOS中的原理分析和運用

開發(fā) 前端 OpenHarmony
ActiveData是一個持有可被觀察數(shù)據(jù)的類,ActiveData需要一個觀察者對象,一般是DataObserver類的具體實現(xiàn)。

[[418313]]

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

在講解ActiveData實現(xiàn)原理之前,我們有必要先了解一下兩個重要的類Lifecycle以及DataObserver,這兩個類在ActiveData整個運行過程中扮演了非常重要的角色。

  • Lifecycle提供了觀察Ability和AbilitySlice的生命周期能力
  • DataObserver通過持有一個Lifecycle對象來觀察Ability或者AbilitySlice的生命周期變化,同時DataObserver還允許ActiveData觀察其生命周期變化,因此DataObserver和ActiveData相互觀察,DataObserver觀察ActiveData的數(shù)據(jù)變化,ActiveData觀察DataObserver的生命周期變化。

ActiveData作用和特點

ActiveData是一個具有感知生命周期能力變化的數(shù)據(jù)通知類組件,非常適合在一些對數(shù)據(jù)同步性較高的場景下使用,它具有以下三個特點。

基于觀察者模式:

ActiveData是一個持有可被觀察數(shù)據(jù)的類,ActiveData需要一個觀察者對象,一般是DataObserver類的具體實現(xiàn)。

感知生命周期:

ActiveData具有生命周期感知能力,目前ActiveData具有兩種通知模式,一種是Ability/AbilitySlice生命周期是活躍(ACTIVE)狀態(tài)時才更新數(shù)據(jù),另一種是Ability/AbilitySlice生命周期處于任何存活狀態(tài)(即只要沒有被銷毀)都可以更新數(shù)據(jù)。

自動解除數(shù)據(jù)訂閱:

ActiveData必須配合實現(xiàn)了Lifecycle的對象使用。當(dāng)Ability/AbilitySlice被銷毀(STOP狀態(tài))后,會自動解除訂閱,這在一定程度上可以避免內(nèi)存泄漏等問題。

實踐

1.基礎(chǔ)用法

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.     private ActiveData<String> activeData; 
  3.  
  4.     private Text mText; 
  5.  
  6.     private final DataObserver<String> dataObserver = new DataObserver<String>() { 
  7.         @Override 
  8.         public void onChanged(String s) { 
  9.             mText.setText(s); 
  10.         } 
  11.     }; 
  12.  
  13.     @Override 
  14.     public void onStart(Intent intent) { 
  15.         super.onStart(intent); 
  16.         super.setUIContent(ResourceTable.Layout_ability_main); 
  17.         activeData = new ActiveData<>(); 
  18.         dataObserver.setLifecycle(getLifecycle()); 
  19.  
  20.         mText = (Text) findComponentById(ResourceTable.Id_text_helloworld); 
  21.  
  22.         subscribe(); 
  23.     } 
  24.  
  25.     private void subscribe() { 
  26.         activeData.addObserver(dataObserver, true); 
  27.     } 
  28.  
  29.     @Override 
  30.     public void onActive() { 
  31.         super.onActive(); 
  32.         activeData.setData("New Hello World"); 
  33.     } 

運行之后的截圖:

【中軟國際】ActiveData在HarmonyOS中的原理分析和運用-鴻蒙HarmonyOS技術(shù)社區(qū)

從運行結(jié)果可以看出,setData調(diào)用后會立即觸發(fā)onChanged回調(diào)方法

2.主線程手動調(diào)用

  1. // 添加如下代碼測試DataObserver的onChanged方法是否會執(zhí)行 
  2. findComponentById(ResourceTable.Id_button) 
  3.                 .setClickedListener(component -> activeData.setData("I Love China")); 

 運行結(jié)果如下:

【中軟國際】ActiveData在HarmonyOS中的原理分析和運用-鴻蒙HarmonyOS技術(shù)社區(qū)

從運行結(jié)果我們可以看到,onChanged方法會一直觸發(fā),并不會因為值相同而不執(zhí)行,雖然暫時看不了鴻蒙源碼,但我們可以大膽猜測,鴻蒙底層維護了一個類似于版本號的標(biāo)記,每次setData,該標(biāo)記會自動+1,從而通過此版本號來判斷data是否有變化,進而決定是否觸發(fā)onChanged回調(diào)方法。

3.子線程調(diào)用

  1. @Override 
  2.    public void onActive() { 
  3.        super.onActive(); 
  4.        new Thread(() -> activeData.setData("New Hello World")).start(); 
  5.    }  

4.運行后發(fā)現(xiàn)沒有問題,可以正常調(diào)用,說明setData方法可以在子線程調(diào)用。

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.     private ActiveData<String> activeData; 
  3.     private ActiveData<String> activeData2; 
  4.  
  5.     private Text mText; 
  6.  
  7.     private final DataObserver<String> dataObserver = new DataObserver<String>() { 
  8.         @Override 
  9.         public void onChanged(String s) { 
  10.             mText.setText(s); 
  11.             System.out.println("ActiveData:---onChange:"+s); 
  12.         } 
  13.     }; 
  14.  
  15.     private final DataObserver<String> dataObserver2 = new DataObserver<String>() { 
  16.         @Override 
  17.         public void onChanged(String s) { 
  18.             mText.setText(s); 
  19.             System.out.println("ActiveData:---onChange:"+s); 
  20.         } 
  21.     }; 
  22.  
  23.     @Override 
  24.     public void onStart(Intent intent) { 
  25.         super.onStart(intent); 
  26.         super.setUIContent(ResourceTable.Layout_ability_main); 
  27.         activeData = new ActiveData<>(); 
  28.         activeData2 = new ActiveData<>(); 
  29.  
  30.         dataObserver.setLifecycle(getLifecycle()); 
  31.         dataObserver2.setLifecycle(getLifecycle()); 
  32.  
  33.         mText = (Text) findComponentById(ResourceTable.Id_text_helloworld); 
  34.         findComponentById(ResourceTable.Id_button) 
  35.                 .setClickedListener(component -> activeData.setData("I Love China")); 
  36.  
  37.         findComponentById(ResourceTable.Id_addObserver_true).setClickedListener(component -> { 
  38.             System.out.println("ActiveData:-------------"); 
  39.             Intent intent1 = new Intent(); 
  40.             Operation operation = new Intent.OperationBuilder() 
  41.                     .withDeviceId(""
  42.                     .withBundleName(getBundleName()) 
  43.                     .withAbilityName(SecondAbility.class.getName()) 
  44.                     .build(); 
  45.  
  46.             intent1.setOperation(operation); 
  47.             startAbility(intent1); 
  48.             // 此處是為了驗證Ability在inActive狀態(tài)的值的變化情況 
  49.             new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> activeData.setData("New Hello World"), 2000); 
  50.         }); 
  51.  
  52.         findComponentById(ResourceTable.Id_addObserver_false).setClickedListener(component -> { 
  53.             System.out.println("ActiveData:-------------"); 
  54.             Intent intent1 = new Intent(); 
  55.             Operation operation = new Intent.OperationBuilder() 
  56.                     .withDeviceId(""
  57.                     .withBundleName(getBundleName()) 
  58.                     .withAbilityName(SecondAbility.class.getName()) 
  59.                     .build(); 
  60.  
  61.             intent1.setOperation(operation); 
  62.             startAbility(intent1); 
  63.             // 此處是為了驗證Ability在inActive狀態(tài)的值的變化情況 
  64.             new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> activeData2.setData("New Hello World"), 2000); 
  65.         }); 
  66.  
  67.         subscribe(); 
  68.     } 
  69.  
  70.     private void subscribe() { 
  71.         activeData.addObserver(dataObserver, true); 
  72.         activeData2.addObserver(dataObserver, false); 
  73.     } 
  74.  
  75.     @Override 
  76.     public void onActive() { 
  77.         super.onActive(); 
  78.         System.out.println("ActiveData:---onActive"); 
  79.     } 
  80.  
  81.     @Override 
  82.     protected void onInactive() { 
  83.         super.onInactive(); 
  84.         System.out.println("ActiveData:---onInactive"); 
  85.     } 
  86.  
  87.     @Override 
  88.     protected void onBackground() { 
  89.         super.onBackground(); 
  90.         System.out.println("ActiveData:---onBackground"); 
  91.     } 
  92.  
  93.     @Override 
  94.     public void onForeground(Intent intent) { 
  95.         super.onForeground(intent); 
  96.     System.out.println("ActiveData:---onForeground"); 
  97.     } 

運行效果如下: 

從以上運行結(jié)果,可以看出addObserver(dataObserver, true/false)方法的特點,當(dāng)為true是表示無論Ability/AbilitySlice處于任何生命周期狀態(tài),均會觸發(fā)onChanged回調(diào)方法,當(dāng)為false時表示Ability/AbilitySlice只有處于ACTIVE狀態(tài)時才會觸發(fā)onChanged方法。

總結(jié)

  • ActiveData內(nèi)部是依靠Lifecycle來感知組件的生命周期,從而可以避免內(nèi)部泄漏
  • 開發(fā)者無需維護observer對象,當(dāng)Ability/AbilitySlice被銷毀時,相關(guān)聯(lián)的observer會被自動移除
  • 當(dāng)Ability/AbilitySlice處于活躍(ACTIVE)狀態(tài)時,當(dāng)ActiveData數(shù)據(jù)源發(fā)生變化時onChanged方法會立即觸發(fā),去更新UI或者執(zhí)行我們想要的任何操作
  • setData方法可在任意線程中去調(diào)用,開發(fā)者無需關(guān)心調(diào)用者是否在主線程中
  • setData方法即使設(shè)置同樣的數(shù)據(jù)對象,onChanged方法仍然會被觸發(fā)

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

 

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

2017-01-17 09:38:52

ZooKeeperHadoopHBase

2016-09-12 14:33:20

javaHashMap

2015-07-07 18:15:42

集群負(fù)載均衡云計算

2021-01-21 05:46:22

JavaLambda開發(fā)

2011-01-11 10:21:56

TCPIP流量整形

2021-07-16 11:40:58

鴻蒙HarmonyOS應(yīng)用

2015-10-13 10:16:01

數(shù)據(jù)分析淘寶地域

2010-04-13 12:32:38

Oracle字符集

2017-08-17 09:49:06

云存儲技術(shù)運用

2015-06-15 10:12:36

Java原理分析

2021-12-14 10:24:44

可見性網(wǎng)絡(luò)安全零信任

2025-01-23 11:18:22

JavaSPI接口

2017-08-01 09:37:00

深度學(xué)習(xí)美團機器學(xué)習(xí)

2009-02-27 08:56:30

IIS.Net原理分析

2024-06-19 16:02:46

2011-05-04 15:21:20

swing

2017-05-17 08:51:39

WebView分析應(yīng)用

2009-03-26 13:43:59

實現(xiàn)Order ByMySQL

2023-05-09 16:11:05

2023-11-29 16:21:30

Kubernetes服務(wù)注冊
點贊
收藏

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