Anko doAsyncResult 协程
Anko doAsyncResult coroutines
我是 anko 和协同程序的新手,所以如果我问一些琐碎的问题,请原谅:)
所以我想做的是让用户单击一个按钮,然后我想从互联网上下载 JSON,将其存储在本地并解析它。由于这两个操作都需要相当长的时间,所以我想使用 anko 协程。
所以第一个问题是:
1.我可以使用嵌套的 doAsync 调用,在第一个 UIThread 中调用第二个 doAsync 吗?
我试过了,它似乎有效,但感觉不对,所以我试图找到一种更优雅的方式
示例:
doAsync {
downloadFileFromUrl(fileUrl)
uiThread {
doAsync {
IOUtils.parseFile(context!!)
val database = AppDatabase.getInstance(context!!)
val results = database.resultsDao().all
uiThread {
//show Results
}
}
}
}
2。在为我的问题寻找解决方案时,我发现了 doAsyncResult。如果 1 不正确,这是正确的方法吗?我已经尝试过使用它,但使用布尔值时出现错误。见下文:
private fun downloadFileFromUrl(fileUrl: String): Boolean {
try{
//Download file. No doAsync calls here.
//The procedure just returns true if successful or false in case of any errors
return true
} catch (e: Exception) {
Log.e("Error: ", e.message)
return false
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
parseButton.setOnClickListener {
try {
val downloadFileResult: (AnkoAsyncContext<Boolean>.() -> Boolean) = {
::downloadFileFromUrl.invoke(fileUrl)
}
val downloadFileResultFutureValue: Future<Boolean> = doAsyncResult(null, downloadFileResult)
//Continue processing if downloadFileResultFutureValue is true
} catch (e: IOException) {
e.printStackTrace()
}
}
}
这一行
val downloadFileResultFutureValue: Future<Boolean> = doAsyncResult(null, downloadFileResult)
编译时出现以下错误,我不明白如何修复:
Type inference failed: Cannot infer type parameter T in
fun <T, R> T.doAsyncResult
(
exceptionHandler: ((Throwable) → Unit)? = ...,
task: AnkoAsyncContext<T>.() → R
)
: Future<R>
None of the following substitutions
receiver: Boolean
arguments:
(
((Throwable) → Unit)?,
AnkoAsyncContext<Boolean>.() → Boolean
)
receiver: BlankFragment
arguments:
(
((Throwable) → Unit)?,
AnkoAsyncContext<BlankFragment>.() → Boolean
)
can be applied to
receiver: BlankFragment
arguments:
(
Nothing?,
AnkoAsyncContext<Boolean>.() → Boolean
)
提前致谢
这样做:
doAsync {
// 1. Something
uiThread {
// 2. Nothing
doAsync {
确实没有多大意义,除非(2)不是什么都没有,而你只是省略了一些代码。
如果你没有,你可以继续使用这个版本:
doAsync {
downloadFileFromUrl(fileUrl)
IOUtils.parseFile(context!!)
val database = AppDatabase.getInstance(context!!)
val results = database.resultsDao().all
uiThread {
//show Results
}
}
由于 parseFile()
无论如何都依赖于 downloadFileFromUrl()
,并且一切都在协程中运行,因此来回添加此操作并不会提高并发性。
我是 anko 和协同程序的新手,所以如果我问一些琐碎的问题,请原谅:)
所以我想做的是让用户单击一个按钮,然后我想从互联网上下载 JSON,将其存储在本地并解析它。由于这两个操作都需要相当长的时间,所以我想使用 anko 协程。
所以第一个问题是:
1.我可以使用嵌套的 doAsync 调用,在第一个 UIThread 中调用第二个 doAsync 吗? 我试过了,它似乎有效,但感觉不对,所以我试图找到一种更优雅的方式
示例:
doAsync {
downloadFileFromUrl(fileUrl)
uiThread {
doAsync {
IOUtils.parseFile(context!!)
val database = AppDatabase.getInstance(context!!)
val results = database.resultsDao().all
uiThread {
//show Results
}
}
}
}
2。在为我的问题寻找解决方案时,我发现了 doAsyncResult。如果 1 不正确,这是正确的方法吗?我已经尝试过使用它,但使用布尔值时出现错误。见下文:
private fun downloadFileFromUrl(fileUrl: String): Boolean {
try{
//Download file. No doAsync calls here.
//The procedure just returns true if successful or false in case of any errors
return true
} catch (e: Exception) {
Log.e("Error: ", e.message)
return false
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
parseButton.setOnClickListener {
try {
val downloadFileResult: (AnkoAsyncContext<Boolean>.() -> Boolean) = {
::downloadFileFromUrl.invoke(fileUrl)
}
val downloadFileResultFutureValue: Future<Boolean> = doAsyncResult(null, downloadFileResult)
//Continue processing if downloadFileResultFutureValue is true
} catch (e: IOException) {
e.printStackTrace()
}
}
}
这一行
val downloadFileResultFutureValue: Future<Boolean> = doAsyncResult(null, downloadFileResult)
编译时出现以下错误,我不明白如何修复:
Type inference failed: Cannot infer type parameter T in
fun <T, R> T.doAsyncResult
(
exceptionHandler: ((Throwable) → Unit)? = ...,
task: AnkoAsyncContext<T>.() → R
)
: Future<R>
None of the following substitutions
receiver: Boolean
arguments:
(
((Throwable) → Unit)?,
AnkoAsyncContext<Boolean>.() → Boolean
)
receiver: BlankFragment
arguments:
(
((Throwable) → Unit)?,
AnkoAsyncContext<BlankFragment>.() → Boolean
)
can be applied to
receiver: BlankFragment
arguments:
(
Nothing?,
AnkoAsyncContext<Boolean>.() → Boolean
)
提前致谢
这样做:
doAsync {
// 1. Something
uiThread {
// 2. Nothing
doAsync {
确实没有多大意义,除非(2)不是什么都没有,而你只是省略了一些代码。
如果你没有,你可以继续使用这个版本:
doAsync {
downloadFileFromUrl(fileUrl)
IOUtils.parseFile(context!!)
val database = AppDatabase.getInstance(context!!)
val results = database.resultsDao().all
uiThread {
//show Results
}
}
由于 parseFile()
无论如何都依赖于 downloadFileFromUrl()
,并且一切都在协程中运行,因此来回添加此操作并不会提高并发性。