All Projects → liujingxing → rxlife-coroutine

liujingxing / rxlife-coroutine

Licence: other
[停止维护]自动管理协程生命周期,并自动捕获异常

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to rxlife-coroutine

Fiber Ext
stackful-coroutines for PHP
Stars: ✭ 142 (+425.93%)
Mutual labels:  coroutine
Mvvmtemplate
An Android Template with MVVM and Clean Architecture
Stars: ✭ 182 (+574.07%)
Mutual labels:  coroutine
swoole-examples
Examples on how to use the Swoole async PHP framework
Stars: ✭ 64 (+137.04%)
Mutual labels:  coroutine
Guzzle Swoole
让基于 Guzzle 的项目完美无缝兼容 Swoole 协程,支持:Guzzle、Elasticsearch client——来自宇润 PHP 全家桶
Stars: ✭ 143 (+429.63%)
Mutual labels:  coroutine
Hyperf Skeleton
🛠 A skeleton of Hyperf framework that provided by official team
Stars: ✭ 162 (+500%)
Mutual labels:  coroutine
Libgo
Go-style concurrency in C++11
Stars: ✭ 2,521 (+9237.04%)
Mutual labels:  coroutine
Unitask
Provides an efficient allocation free async/await integration for Unity.
Stars: ✭ 2,547 (+9333.33%)
Mutual labels:  coroutine
YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+614.81%)
Mutual labels:  coroutine
Minicoro
Single header asymmetric stackful cross-platform coroutine library in pure C.
Stars: ✭ 164 (+507.41%)
Mutual labels:  coroutine
raster
A micro server framework, support coroutine, and parallel-computing, used for building flatbuffers/thrift/protobuf/http protocol service.
Stars: ✭ 19 (-29.63%)
Mutual labels:  coroutine
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (+429.63%)
Mutual labels:  coroutine
Yii2 Swoole
make yii2 project runing on swoole
Stars: ✭ 161 (+496.3%)
Mutual labels:  coroutine
Trip
Async HTTP for Humans, coroutine Requests ⛺️
Stars: ✭ 211 (+681.48%)
Mutual labels:  coroutine
Aint Queue
🚀 An async-queue library built on top of swoole, flexable multi-consumer, coroutine supported. 基于 Swoole 的一个异步队列库,可弹性伸缩的工作进程池,工作进程协程支持。
Stars: ✭ 143 (+429.63%)
Mutual labels:  coroutine
Rainlayout
Constraintlayout based rain-animation view developed backed by Kotlin Coroutines.
Stars: ✭ 32 (+18.52%)
Mutual labels:  coroutine
Ext Zookeeper
🧑 Coroutine-based ZooKeeper Client for PHP
Stars: ✭ 140 (+418.52%)
Mutual labels:  coroutine
Yurunhttp
YurunHttp 是开源的 PHP HTTP 客户端,支持链式操作,简单易用。完美支持Curl、Swoole 协程。QQ群:17916227
Stars: ✭ 197 (+629.63%)
Mutual labels:  coroutine
hpc
Learning and practice of high performance computing (CUDA, Vulkan, OpenCL, OpenMP, TBB, SSE/AVX, NEON, MPI, coroutines, etc. )
Stars: ✭ 39 (+44.44%)
Mutual labels:  coroutine
universe-topology
A universal computer knowledge topology for all the programmers worldwide.
Stars: ✭ 47 (+74.07%)
Mutual labels:  coroutine
Libaco
A blazing fast and lightweight C asymmetric coroutine library 💎 ⛅🚀⛅🌞
Stars: ✭ 2,918 (+10707.41%)
Mutual labels:  coroutine

该项目已停止维护,正式被抛弃,原因如下:

1、同一个FragmentActivity/Fragment下,rxLifeScopelifecycleScope不能共用;同一个ViewModel下,rxLifeScopeviewModelScope不能共用

2、配合RxHttp发请求时,每次都要开启一个协程来捕获异常,这对于再次封装的人,非常不友好;
目前RxHttpawaitResult操作符一样可以捕获异常,所以rxLifeScope就没了用武之地,是时候退出历史舞台了

3、不能同lifecycleScopeviewModelScope一样,开启协程时,传入CoroutineContextCoroutineStart参数
亦没有一系列launchXxx方法

4、rxLifeScope配合RxHttp v2.6.6及以上版本发请求时,调用async方法将导致请求结束回调不被调用

替代品

//lifecycleScope                                                  
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"   
//viewModelScope                                                  
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" 

使用

//使用lifecycleScope或viewModelScope开协程,更多用法,请自行查阅相关资料
lifecycleScope.launch {      
     //请求开始                  
    RxHttp.get("...")        
        .toStr()             
        .awaitResult {       
             //请求成功          
        }.onFailure {        
             //请求异常          
        }                    
    //请求结束                   
}                            

其中awaitResult只是RxHttp的一种异常处理操作符,更多异常操作符,请查看RxHttp ,比Retrofit 更优雅的协程体验

以上代码,同样可以做到页面销毁,协程自动关闭,协程关闭,请求跟着关闭

更新日志

v2.2.0 (2021-09-01)

  • rxLifeScope标记为过时,正式退出历史舞台

v2.0.1 (2020-09-14)

  • 修改:协程被关闭后,若有异常,不再走失败回调

  • 修复:捕获失败回调里的异常,并打印日志

简介

RxLife-Coroutine 优势

  • 开启协程,并自动管理协程生命周期,在页面销毁时,自动关闭协程

  • 自动捕获协程异常,通过回调可拿到异常信息

  • 可监听协程开启及结束

gradle依赖

dependencies {
   //管理协程生命周期,页面销毁,自动关闭协程
   implementation 'com.github.liujingxing.rxlife:rxlife-coroutine:2.2.0'
}

使用

FragmentActivity/Fragment/ViewModel环境下

rxLifeScope.launch({
    //协程体
}, {
    //异常回调
}, {
    //协程开始回调
}, {
    //协程结束回调,不管成功/失败都会回调
})

以上代码,有两点需要注意

  • launch方法有共有4个参数,仅有第一个参数是必须的,其它3个参数都有默认值,可以不传

  • 我们无需手动关闭协程,在页面销毁时,会自动关闭协程,以防止内存泄露

非FragmentActivity/Fragment/ViewModel环境下

val job = RxLifeScope().launch({
    //协程体
}, {
    //异常回调
}, {
    //协程开始回调
}, {
    //协程结束回调,不管成功/失败都会回调
})

//在合适的时机,手动关闭协程
job.cancel()

以上代码使用RxLifeScope()手动创建了RxLifeScope对象,这种方式,我们需要在合适的时机,拿到Job对象,关闭协程

Licenses

Copyright 2020 liujingxing

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].