本项目是基于开源项目AutoDispose进行ohos化的移植和开发的,可以通过项目标签以及github地址(https://github.com/uber/AutoDispose)追踪到原项目版本
下载依赖库har包autoDispose.har。
启动 DevEco Studio,将下载的har包,导入工程目录“entry->libs”下。
在moudle级别下的build.gradle文件中添加依赖,在dependences标签中增加对libs目录下har包的引用。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
……
}
在导入的har包上点击右键,选择“Add as Library”对包进行引用,选择需要引用的模块,并点击“OK”即引用成功。
在工程的build.gradle的allprojects中,添加HAR所在的Maven仓地址
repositories {
maven {
url 'http://106.15.92.248:8081/repository/Releases/'
}
}
在应用模块的build.gradle的dependencies闭包中,添加如下代码:
dependencies {
implementation 'com.uber.autodispose2:autodispose:1.0.1'
}
//在Ability中使用
myObservable
.map(...)
.doOnDispose(() -> HiLog.info(Label, "Disposing subscription from onActive()"))
.subscribeOn(...)
.observeOn(...)
.as(AutoDispose.autoDisposable(ohosLifecycleScopeProvider.from(this))
.subscribe(s -> ...);
通过给Observable 添加这行代码:
```
as(AutoDispose.autoDisposable(ohosLifecycleScopeProvider.from(lifecycleOwner))
```
该Observable就会在Ability的onBackground()生命周期时,自动解除订阅,以防止因生命周期组件的生命周期而导致的RxJava内存泄漏事件。
这行代码用于监听Rxjava流结束时回掉
.doOnDispose(() ->
HiLog.info(Label, "Disposing subscription from onActive()"))
Apache Licence Copyright (C) 2017 Uber Technologies
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。