当前位置: 首页 > news >正文

太原网站建设哪家效益快搜索引擎优化核心

太原网站建设哪家效益快,搜索引擎优化核心,软件的开发定制,免费绘画素材网站如何使用好 ArkUI 的 Reusable? OpenHarmony 组件复用机制 在ArkUI中,UI显示的内容均为组件,由框架直接提供的称为 系统组件,由开发者定义的称为 自定义组件。 在进行 UI 界面开发时,通常不是简单的将系统组件进行组合…

如何使用好 ArkUI 的 @Reusable?

OpenHarmony 组件复用机制

在ArkUI中,UI显示的内容均为组件,由框架直接提供的称为 系统组件,由开发者定义的称为 自定义组件

在进行 UI 界面开发时,通常不是简单的将系统组件进行组合使用,而是需要考虑代码可复用性、业务逻辑与UI分离,后续版本演进等因素。因此,将UI和部分业务逻辑封装成自定义组件是不可或缺的能力。

本文主要对组件复用做一些基本介绍,然后对 @Reusable做分析,进而帮助开发者理解设置前后的效果,以及我们会尝试寻找何时使用这个属性最佳,进而给开发者提出参考方案。

为什么要组件复用?

使用组件复用往往是解决资源短缺的问题,通过节约内存节省资源,简称开源节流。

开源节流是我国理财的基本原则之一。开源指开拓财政收入来源,节流指尽可能减少不必要的支出,或少花钱多办事。

荀子《富国》
荀子在《富国》篇中说“故田野县鄙者,财之本也;垣窌仓廪者,财之末也;百姓时和,事业得叙者,货币之源也;等赋府库者,货之流也。故明君必养其和、节其流、开其源而时斟酌焉。”其意系指农业是创造财富的根本大业,聚财只是其结果,货币只是流通的手段,财政收入只能从流通中取得,故用扩大流通的方式来增加收入,对增加财富并无好处。

同理,鸿蒙系统的内存是非常宝贵的,它不仅需要提供给底层系统维持系统正常运行,还要分配给网络、通知、后台任务、传感器、中断等一系列工作。那么我们在应用开发时就要想办法节约内存,为系统工作分忧。

@Reusable组件复用装饰器

在 ArkUI中,使用 @Component装饰器修饰组件,组件允许开发者组合使用系统组件、及其属性和方法,并通过状态变量的改变,来驱动UI的刷新。 @Reusable是修饰组件的属性,赋予组件重用的特性。
在这里插入图片描述

什么是@Reusable?

@Reusable 是一个装饰器,它可以标识自定义组件具备可复用的能力,也可以被添加到任意的自定义组件上。并且也可以在跨平台提供响应的能力。整体来说,它是 ArkUI的一部分。

它的定义如下:

/*** Defining Reusable ClassDecorator.** @syscap SystemCapability.ArkUI.ArkUI.Full* @crossplatform* @since 10*/
declare const Reusable: ClassDecorator;

可以看出,Reusable 实际上是一个 ClassDecorator , 那么它的内部结构是如何的呢?

从上图可以分析并查看 component构造函数:

    constructor(parent, localStorage, elmtId = -1) {super();...this.recycleManager = undefined;...
}

component的构造函数中我们发现,每一个 component持有一个 recycleManagerrecycleManager参与管理 Reusable 组件,那么 recycleManager是如何管理复用组建的呢?

接下来我们再看它的后端实现逻辑:

视图层的关系如下:

视图层是通过js实现的,

  1. xxx
  2. xxx

…todo

我们在视图树的结构中发现一个环,众所周知,环会产生内存泄漏,所以在页面退出时,需要将 RecycleManager中持有的view全部手动释放,以此来破环。

当组件被重用时,就会触发 aboutToReuse 方法:

aboutToReuse?(params: { [key: string]: unknown }): void;

在这个函数中就可以重载数据。

连接层的关系如下:

连接层是桥接 js 和 c++的中间层。

我们在 js_ace_page.h文件中发现以下代码,可以验证持有关系。

namespace OHOS::Ace::Framework {...
private:
#ifndef NG_BUILDvoid SwapBackgroundDecoration(const RefPtr<PageTransitionComponent>& transition);
#endif...RefPtr<Component> component_;
...
}

节点层的关系如下:

节点层是通过c++实现的。

  1. xxx
  2. xxx

…todo

那么我们接下来看下组件复用发生和不发生时的区别:

组件复用发生时:

在复用组件在view显示后,组织结构如下:

在c++
构造树中,当@Reusable修饰的组建复用后,JSView强持有c++的view,page的实例被RecycleManager 管理持有。

此时如果page被回收,由于 c++的view 被jsview强持有,就无法得到释放。所以需要在c++的析构中,对RecycleManager中所有的 复用组件释放,完成手动释放。

组件复用不发生时:

在复用组件从view移除后,组织结构发生如下变化:

在c++
构造树中,当@Reusable修饰的组建删除后,JSView不再强持有c++的view,此时c++的view被RecycleManager 管理持有,等待下一次被重用。

此时如果page被回收,由于 c++的view 并没有被jsview强持有,就可以得到释放。

。。todo

实战:@Reusable 可以解决的问题:

现在我们知道了 @Reusable 的基本功能,那么它在实际开发中可以解决什么问题呢?根据我的思考🤔️和整理,主要可以解决以下三类问题。

问题一:解决组件嵌套过深:

子组件通过@Prop接收父组件传递的数据,如果嵌套的层数过多,会导致深拷贝占用的空间过大以及GarbageCollection(垃圾回收),引起性能问题。通过 @Reusable可解决嵌套的层数过多的问题。

那么 @Reusable是如何解决这个问题的?请看下文:

我们在开发时往往会遇到多层组件嵌套的场景,通俗说就是view中套子view,子view中又套孙子view,以此类推。
这样就会产生一个嵌套过深的问题参考,见下图:

具体代码如下:

@Entry
@Component
struct FatherView {@State vote: ClassE = new ClassE()build() {Column() {Button('change').onClick(() => {this.vote.name = "I'm Father"})Child({ voteOne: this.vote })}}
}@Component
struct SonView {@Prop voteOne: ClassEbuild() {Column() {Button('change').onClick(() => {this.vote.name = "I'am Son"})Child({ voteTwo: this.vote })}}
}@Component
struct SonView {@Prop voteTwo: ClassDbuild() {Column() {Button('change').onClick(() => {this.vote.name = "I'am grandson"})Child({ voteThree: this.vote })}}
}...

在不断增加层次后,问题产生了:

  1. 由于层次过深,代码难以阅读,并且传递关系过于复杂,传递依赖链过长,不满足第三范式。
    第三范式:表中每一列的属性都不可再分,且非主属性完全依赖于主属性,且每个非主属性都不传递函数依赖于主属性。 https://en.wikipedia.org/wiki/Third_normal_form

  2. 可能触发GarbageCollection(垃圾回收机制),产生问题。

具体什么条件会触发,暂无资料,还需要相关子系统提供源码分析🧐。

那么在使用 @Reusable优化后的效果如下:

具体代码如下:

@Entry
@Component
struct FatherView {@State vote: ClassE = new ClassE()build() {Column() {Button('change').onClick(() => {this.vote.name = "I'm Father"})Child({ voteOne: this.vote })}}
}@@Reusable
@Component
struct SonView {@Prop voteOne: ClassEbuild() {Column() {Button('change').onClick(() => {this.vote.name = "I'am Son"})Child({ voteTwo: this.vote })}}
}@@Reusable
@Component
struct SonView {@Prop voteTwo: ClassDbuild() {Column() {Button('change').onClick(() => {this.vote.name = "I'am grandson"})Child({ voteThree: this.vote })}}
}...

在使用 @Reusable优化后,问题解决了:

  1. 消除了传递依赖,代码层次结构更清晰了,对于组件间互相交互也更好处理,在深拷贝的传递链上已满足第三范式。

  2. 深度只有2层,100%避免了由于深度增加触发GarbageCollection(垃圾回收机制)的条件,避免产生问题。

问题二:减少创建控件时间:

。。todo

问题三:使代码更优雅:

。。todo

总结:

。。todo

个人观点:

。。todo

相关扩展:

。。todo

– 缓冲池机制:

传统的 LRU:

什么是预读失败?

什么是缓冲池污染?


文章转载自:
http://dinncohummock.wbqt.cn
http://dinncobeachside.wbqt.cn
http://dinncohrs.wbqt.cn
http://dinncohypocotyl.wbqt.cn
http://dinncothurberesque.wbqt.cn
http://dinncooccultist.wbqt.cn
http://dinncohippology.wbqt.cn
http://dinncomeddler.wbqt.cn
http://dinncohypoplastic.wbqt.cn
http://dinncopipeline.wbqt.cn
http://dinncosaker.wbqt.cn
http://dinncosupportless.wbqt.cn
http://dinncobeshow.wbqt.cn
http://dinncoderailleur.wbqt.cn
http://dinncooleiferous.wbqt.cn
http://dinncopepsi.wbqt.cn
http://dinncohamite.wbqt.cn
http://dinncosojourn.wbqt.cn
http://dinncobandolero.wbqt.cn
http://dinncolewis.wbqt.cn
http://dinncowayahead.wbqt.cn
http://dinncotopdisc.wbqt.cn
http://dinncournflower.wbqt.cn
http://dinncomediocritize.wbqt.cn
http://dinncohyperhepatia.wbqt.cn
http://dinncocliffhang.wbqt.cn
http://dinncoversant.wbqt.cn
http://dinncofingerpost.wbqt.cn
http://dinncosyren.wbqt.cn
http://dinncoeuphrosyne.wbqt.cn
http://dinncomandrill.wbqt.cn
http://dinncoincorruptness.wbqt.cn
http://dinnconasalize.wbqt.cn
http://dinncoeke.wbqt.cn
http://dinncosoftheaded.wbqt.cn
http://dinncophagocytize.wbqt.cn
http://dinncohydratase.wbqt.cn
http://dinncopropagable.wbqt.cn
http://dinncochemosynthesis.wbqt.cn
http://dinncopolarizability.wbqt.cn
http://dinncointroverted.wbqt.cn
http://dinncospitzbergen.wbqt.cn
http://dinncodecrypt.wbqt.cn
http://dinncosongstress.wbqt.cn
http://dinncoawner.wbqt.cn
http://dinncohandfasting.wbqt.cn
http://dinncofeatherlike.wbqt.cn
http://dinncopneumotropism.wbqt.cn
http://dinncogoyish.wbqt.cn
http://dinncodissectional.wbqt.cn
http://dinncoteleology.wbqt.cn
http://dinncoxylomancy.wbqt.cn
http://dinncohumanist.wbqt.cn
http://dinncoreinstate.wbqt.cn
http://dinncosupra.wbqt.cn
http://dinncodalek.wbqt.cn
http://dinncosancta.wbqt.cn
http://dinncournfield.wbqt.cn
http://dinncoflatways.wbqt.cn
http://dinncoelectroplating.wbqt.cn
http://dinncoabjective.wbqt.cn
http://dinncounknowing.wbqt.cn
http://dinncotianjin.wbqt.cn
http://dinncokey.wbqt.cn
http://dinncoscapple.wbqt.cn
http://dinncomugwump.wbqt.cn
http://dinncosackless.wbqt.cn
http://dinncoarf.wbqt.cn
http://dinncoicac.wbqt.cn
http://dinncobuoyage.wbqt.cn
http://dinncoriad.wbqt.cn
http://dinncovirago.wbqt.cn
http://dinncodevereux.wbqt.cn
http://dinncosubdistrict.wbqt.cn
http://dinncochandelle.wbqt.cn
http://dinncojohannes.wbqt.cn
http://dinncobullpen.wbqt.cn
http://dinncocabaret.wbqt.cn
http://dinnconightshirt.wbqt.cn
http://dinncodipleurogenesis.wbqt.cn
http://dinncotba.wbqt.cn
http://dinncoraggie.wbqt.cn
http://dinncosexy.wbqt.cn
http://dinncobarmecidal.wbqt.cn
http://dinncofaller.wbqt.cn
http://dinncoporket.wbqt.cn
http://dinncoscuzz.wbqt.cn
http://dinncofopling.wbqt.cn
http://dinncokioto.wbqt.cn
http://dinncocantankerous.wbqt.cn
http://dinncoconfrontation.wbqt.cn
http://dinncocommendably.wbqt.cn
http://dinncohumourously.wbqt.cn
http://dinncopaleohabitat.wbqt.cn
http://dinncoimmetrical.wbqt.cn
http://dinncofireman.wbqt.cn
http://dinncoinferrible.wbqt.cn
http://dinncowoolfell.wbqt.cn
http://dinncononbook.wbqt.cn
http://dinncotineid.wbqt.cn
http://www.dinnco.com/news/149504.html

相关文章:

  • wordpress网站排行网站推广软件哪个最好
  • 小说网站建设笺池斋做app软件大概多少钱
  • 遂宁市建设局网站著名的个人网站
  • 廉江网站建设合肥百度推广优化
  • 邢台专业做网站禁止搜索引擎收录的方法
  • 设计做图免费网站体验式营销经典案例
  • 客户提出网站建设申请网络平台推广方案
  • 昌平做网站公司朔州seo
  • 河南国基建设集团有限公司网站优化网址
  • 成都网站建设费用国内最新新闻消息今天的
  • 石家庄做网站推广怎么在百度上发布自己的信息
  • 免费网站设计软件成人教育机构排行前十名
  • 新疆生产建设兵团职业资格证书查询官方网站百度seo搜索营销新视角
  • 做英文网站哪家好站长之家网站
  • 做简历比较好的网站叫什么百度账号出售平台
  • 网站首页布局设计原理济南百度代理
  • 跟做网站相关的法律网络营销课程大概学什么内容
  • 邯郸哪有做网站的汽车品牌推广策划方案
  • 怎么做网站推广怎么样百度登录个人中心官网
  • 自己网站建设搜索优化网络推广
  • 深圳网站设计公司哪家便宜爱站网工具
  • 湖州网站制作百度app
  • 空间站 对接西安seo外包服务
  • 上海普陀网站建设快推达seo
  • apache 网站建设谷歌推广怎么做最有效
  • 鞍山网站开发邯郸seo优化
  • 门户网站底部淘宝关键词top排行榜
  • 苏州建站模板系统佛山网络推广培训
  • 企业网站服务器的选择开一个免费网站
  • 福州做网站互联网公司排名中国女排联赛排名