allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mistletoe5215:CoroutineExtension:v1.2.3'
}
-
增加
View.acquireObservableScope()
获取一个跟随窗口移除时取消的协程域 -
增加
Context.acquireLifecycleScope()
从只有context的地方获取一个和lifecycle绑定的协程域,协程block执行完即解绑lifecycle
打包问题修复
revert 使用Jitpack ci 的gradle版本
- 增加CustomMainDispatcher,解决1.3.2版本主协程调度器crash问题
- 增加FJCoroutineDispatcher调度器
FJCoroutineDispatcher
,可以与Dispatchers.IO
做比较
-
对网络请求场景使用超时回收策略调度器
MCoroutineDispatcher
-
删除部分无必要父协程Scope创建
- 支持使用zip表达式进行多个retrofit请求合并
data class ResponseDataModel1(
var code:Int = 0,
var message:String?=null,
var data:String?=null
)
data class ResponseDataModel2(
var code:Int = 0,
var message:String?=null,
var data:String?=null
)
@BaseUrl("www.github.com")
interface ApiService {
@GET("getListData")
fun foo1(@Query param:String):Deferred<ResponseDataModel1>
@GET("getImageData")
fun foo2(@Query param:String):Deferred<ResponseDataModel2>
}
zip(mService.foo1(param), mService.foo2(param))
.bindLifecycle(activity)
.onCompletion {
val resultList = it.awaitAll()
//ensure get all result
if(resultList.size == 2){
Log.d("Mistletoe",(resultList[0] as ResponseDataModel1)?.data.toString() +"\n"+
(resultList[1] as ResponseDataModel2)?.data.toString()
)
}
}.catch {
Log.e("Mistletoe",it.message)
}
- 现在支持中缀表达式进行promise then catch 的流式调用
lifecycleOwner promise{
//请求接口
mApiService.foo(param)
} then {
mResponseDataModel ->
//返回结果,更新UI
} catch { e ->
//捕获异常并在主线程处理异常
}
- 项目使用retrofit v2.6 时
data class ResponseDataModel(
var code:Int = 0,
var message:String?=null,
var data:String?=null
)
@BaseUrl("www.github.com")
interface ApiService {
@GET("getListData")
suspend fun foo(@Query param:String):ResponseDataModel
}
lifecycleOwner.promise{
//请求接口
mApiService.foo(param)
}.then({
mResponseDataModel ->
//返回结果,更新UI
}){ e ->
//捕获异常并处理
}
- 项目使用retrofit v2.3 时
@BaseUrl("www.github.com")
interface ApiService {
@GET("getListData")
fun foo(@Query param:String):Deferred<ResponseDataModel>
}
lifecycleOwner.promise{
//请求接口
mApiService.foo(param)
}.then({
mResponseDataModel ->
//返回结果,更新UI
}){ e ->
//捕获异常并处理
}
val handle = setTimeOut(time = 2000L){
//do delay task
}
// cancel task
handle.cancel()
val handle = setInterval(Dispatchers.IO, 3000L){
//do loop task
}
// cancel task
handle.cancel()
//实现一个3s的倒计时
val mFlowCountDownTimer = FlowCountDownTimer.Builder().from(3)
.to(0)
.interval(1000L)
.withUnit(TimeUnit.SECONDS)
.create()
mFlowCountDownTimer?.apply {
watchStart {
//观察开始动作
}
watchCountDown {
currentCount ->
//观察倒计时
}
watchComplete {
//观察倒计时结束
}
}?.start()
//希望暂停时,
mFlowCountDownTimer?.pause()
//希望恢复倒计时时,
mFlowCountDownTimer?.resume()