Activity中使用kotlin协程的几种写法

一、AppCompatActivity实现CoroutineScope接口:

class Test1Activity : AppCompatActivity(), CoroutineScope {
    private lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        job = Job()
        test()
    }

    private fun test() {
        launch {
            val result = withContext(Dispatchers.IO) {
                delay(1000)
                "0"
            }
            Log.d("result", result)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }
}

二、直接实例化MainScope:

class Test2Activity : AppCompatActivity() {
    private val mailScope = MainScope()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        test()
    }

    private fun test() {
        mailScope.launch {
            val result = withContext(Dispatchers.IO) {
                delay(1000)
                "0"
            }
            Log.d("result", result)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mailScope.cancel()
    }
}

三、导入implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha02',就可以直接在AppCompatActivity调用lifecycleScope:

class Test3Activity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        test()
    }

    private fun test() {
        lifecycleScope.launch {
            val result = withContext(Dispatchers.IO) {
                delay(1000)
                "0"
            }
            Log.d("result", result)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycleScope.cancel()
    }
}

最后发一下使用协程和RxJava的代码量和可读性对比:

协程:

        dialog.show()
        lifecycleScope.launch {
            val result = withContext(Dispatchers.IO) {
                sendHtmlMail()
            }
            dialog.dismiss()
            if (result == OK) {
                makeText(this@EmailReplyActivity, getString(R.string.mail_send_success_hint))
                finish()
            } else if (result == ERR) {
                makeText(this@EmailReplyActivity, getString(R.string.mail_send_fail_hint))
            }
        }

RxJava:

        dialog.show()
        Observable.create { emitter: ObservableEmitter<String?> ->
            val result = sendHtmlMail()
            emitter.onNext(result)
            emitter.onComplete()
        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<String?> {
                    override fun onSubscribe(d: Disposable) {
                        compositeDisposable.add(d)
                    }

                    override fun onNext(value: String?) {
                        if (value.equals(OK)) {
                            makeText(this@EmailReplyActivity, getString(R.string.mail_send_success_hint))
                            finish()
                        } else if (value.equals(ERR)) {
                            makeText(this@EmailReplyActivity, getString(R.string.mail_send_fail_hint))
                        }
                    }

                    override fun onError(e: Throwable) {
                        dialog.dismiss()
                        e.printStackTrace()
                    }

                    override fun onComplete() {
                        dialog.dismiss()
                    }
                })

突然觉得RxJava很恶心有没有

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值