想了解更多内容,请访问:
创新互联建站服务项目包括清涧网站建设、清涧网站制作、清涧网页制作以及清涧网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,清涧网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到清涧省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
和华为官方合作共建的鸿蒙技术社区
https://harmonyos.
在讲解ActiveData实现原理之前,我们有必要先了解一下两个重要的类Lifecycle以及DataObserver,这两个类在ActiveData整个运行过程中扮演了非常重要的角色。
ActiveData是一个具有感知生命周期能力变化的数据通知类组件,非常适合在一些对数据同步性较高的场景下使用,它具有以下三个特点。
ActiveData是一个持有可被观察数据的类,ActiveData需要一个观察者对象,一般是DataObserver类的具体实现。
ActiveData具有生命周期感知能力,目前ActiveData具有两种通知模式,一种是Ability/AbilitySlice生命周期是活跃(ACTIVE)状态时才更新数据,另一种是Ability/AbilitySlice生命周期处于任何存活状态(即只要没有被销毁)都可以更新数据。
ActiveData必须配合实现了Lifecycle的对象使用。当Ability/AbilitySlice被销毁(STOP状态)后,会自动解除订阅,这在一定程度上可以避免内存泄漏等问题。
1.基础用法
- public class MainAbilitySlice extends AbilitySlice {
- private ActiveData
activeData; - private Text mText;
- private final DataObserver
dataObserver = new DataObserver () { - @Override
- public void onChanged(String s) {
- mText.setText(s);
- }
- };
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- activeData = new ActiveData<>();
- dataObserver.setLifecycle(getLifecycle());
- mText = (Text) findComponentById(ResourceTable.Id_text_helloworld);
- subscribe();
- }
- private void subscribe() {
- activeData.addObserver(dataObserver, true);
- }
- @Override
- public void onActive() {
- super.onActive();
- activeData.setData("New Hello World");
- }
- }
运行之后的截图:
从运行结果可以看出,setData调用后会立即触发onChanged回调方法
2.主线程手动调用
- // 添加如下代码测试DataObserver的onChanged方法是否会执行
- findComponentById(ResourceTable.Id_button)
- .setClickedListener(component -> activeData.setData("I Love China"));
运行结果如下:
从运行结果我们可以看到,onChanged方法会一直触发,并不会因为值相同而不执行,虽然暂时看不了鸿蒙源码,但我们可以大胆猜测,鸿蒙底层维护了一个类似于版本号的标记,每次setData,该标记会自动+1,从而通过此版本号来判断data是否有变化,进而决定是否触发onChanged回调方法。
3.子线程调用
- @Override
- public void onActive() {
- super.onActive();
- new Thread(() -> activeData.setData("New Hello World")).start();
- }
4.运行后发现没有问题,可以正常调用,说明setData方法可以在子线程调用。
- public class MainAbilitySlice extends AbilitySlice {
- private ActiveData
activeData; - private ActiveData
activeData2; - private Text mText;
- private final DataObserver
dataObserver = new DataObserver () { - @Override
- public void onChanged(String s) {
- mText.setText(s);
- System.out.println("ActiveData:---onChange:"+s);
- }
- };
- private final DataObserver
dataObserver2 = new DataObserver () { - @Override
- public void onChanged(String s) {
- mText.setText(s);
- System.out.println("ActiveData:---onChange:"+s);
- }
- };
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- activeData = new ActiveData<>();
- activeData2 = new ActiveData<>();
- dataObserver.setLifecycle(getLifecycle());
- dataObserver2.setLifecycle(getLifecycle());
- mText = (Text) findComponentById(ResourceTable.Id_text_helloworld);
- findComponentById(ResourceTable.Id_button)
- .setClickedListener(component -> activeData.setData("I Love China"));
- findComponentById(ResourceTable.Id_addObserver_true).setClickedListener(component -> {
- System.out.println("ActiveData:-------------");
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName(getBundleName())
- .withAbilityName(SecondAbility.class.getName())
- .build();
- intent1.setOperation(operation);
- startAbility(intent1);
- // 此处是为了验证Ability在inActive状态的值的变化情况
- new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> activeData.setData("New Hello World"), 2000);
- });
- findComponentById(ResourceTable.Id_addObserver_false).setClickedListener(component -> {
- System.out.println("ActiveData:-------------");
- Intent intent1 = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName(getBundleName())
- .withAbilityName(SecondAbility.class.getName())
- .build();
- intent1.setOperation(operation);
- startAbility(intent1);
- // 此处是为了验证Ability在inActive状态的值的变化情况
- new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> activeData2.setData("New Hello World"), 2000);
- });
- subscribe();
- }
- private void subscribe() {
- activeData.addObserver(dataObserver, true);
- activeData2.addObserver(dataObserver, false);
- }
- @Override
- public void onActive() {
- super.onActive();
- System.out.println("ActiveData:---onActive");
- }
- @Override
- protected void onInactive() {
- super.onInactive();
- System.out.println("ActiveData:---onInactive");
- }
- @Override
- protected void onBackground() {
- super.onBackground();
- System.out.println("ActiveData:---onBackground");
- }
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- System.out.println("ActiveData:---onForeground");
- }
- }
运行效果如下:
从以上运行结果,可以看出addObserver(dataObserver, true/false)方法的特点,当为true是表示无论Ability/AbilitySlice处于任何生命周期状态,均会触发onChanged回调方法,当为false时表示Ability/AbilitySlice只有处于ACTIVE状态时才会触发onChanged方法。
想了解更多内容,请访问:
和华为官方合作共建的鸿蒙技术社区
https://harmonyos.
分享标题:ActiveData在HarmonyOS中的原理分析和运用
本文链接:http://www.shufengxianlan.com/qtweb/news43/20393.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联