【发布时间】:2018-10-29 23:01:58
【问题描述】:
我有一些代码 1] 获取 JSON 文件,2] 根据该 JSON 文件的内容,获取另一个 JSON 文件。它使用ion 库。
代码大致如下所示(旁注 - 此代码在我的活动的 onCreate 方法上,e 是 Exception 类型的对象):
Ion.with(applicationContext)
.load("someURL")
.asJsonObject()
.setCallback { e, result->
//Do synchronous stuff with the "result" and "e" variables
//that determines whether boolean someCondition is true or false
if(somecondition) {
Ion.with(applicationContext)
.load("https://raw.githubusercontent.com/vedantroy/image-test/master/index.json")
.asJsonObject()
.setCallback { e, result ->
//Some synchronous code in here
}
}
}
这段代码很丑,我想改进它以摆脱嵌套。那有可能吗,如果可以,我该怎么做?
我一直在试验,看来我可以像这样在setCallback 之后调用方法.then(),其中.then() 接受一个callback 对象的参数:
Ion.with(applicationContext).load("someURL").setCallback { //Lambda Stuff }.then()
所以也许这将成为解决方案的一部分,但我不确定。
【问题讨论】:
标签: android kotlin asynccallback ion