【发布时间】:2020-09-16 05:24:57
【问题描述】:
我的目标是获得符合此签名 Deferred<HashMap<Long, String>?> 的输出,或者可能更好的类似 Job 等。不确定在 kotlin 中做事的标准方式以及如何调用/使用该函数,是我是新手。
以下是我在模型层中使用的草稿。如果有人能告诉我做异步事情的正确方法,我将不胜感激。
fun findAll(): Deferred<HashMap<Long, String>?> {
GlobalScope.launch(Dispatchers.IO) {
async {
var roles: HashMap<Long, String>? = null
connection.readableDatabase.query("role", null, null, null, null, null, null).use { cursor ->
if (cursor.count > 0) roles = HashMap()
while (cursor.moveToNext()) {
roles?.put(cursor.getLong(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name")))
}
}
return roles
}
}
}
【问题讨论】:
标签: android kotlin kotlin-coroutines