【发布时间】:2023-04-03 11:34:01
【问题描述】:
是否可以摆脱嵌套的 Task 泛型?
对于每个continueWith 语句,都会在类型中添加一个新的Task。每个延续都是类型的一部分。理想情况下,我会返回一个任务,该任务连续执行每个任务并成功或失败。
示例
第一个操作查询用户的组
private fun getGroupsSnapshot(): Task<QuerySnapshot> {
val userId = Auth.currentUser()!!.uid
val query = userGroupsQuery(groupsCollection, userId)
return query.get()
}
第二个操作查询这些组中的相册。
fun getAlbums(): Task<Task<List<Album>>> {
return getGroupsSnapshot().continueWith { task ->
val documentSnapshots = TaskUtils.getResult(task)
val albums = mutableListOf<Album>()
val fetchAlbumTasks = documentSnapshots.documents.map { document ->
Log.d(TAG, document["name"].toString())
document.reference.collection("albums").get().addOnCompleteListener { queryTask ->
albums.addAll(toObjects(Album::class.java, queryTask))
}
}
return@continueWith Tasks.whenAll(fetchAlbumTasks).continueWith allTasks@ {
return@allTasks albums as List<Album>
}
}
}
但是我希望此操作返回类型 Task<List<Album>> 以保持界面清洁。
【问题讨论】:
-
-
谢谢!我会读这个。
标签: android kotlin google-cloud-firestore google-tasks-api