如何返回失败的任务导致继续执行任务? [英] How to return failed task result in continuation task?
问题描述
我正在Kotlin使用Google的Task API,并且面临下一种情况:
I am using Google's Task API in Kotlin and am faced with the next situation:
...
val deleteTask = getItem(id)?.continueWithTask { task ->
if (task.isSuccessful)
task.result?.toObject(ItemModel::class.java)?.let { deleteFiles(it.media) }
}
deleteTask?.continueWithTask { task ->
if (task.isSuccessful) doSomething()
} ?: doSomething()
...
其中getItem(id)
返回Firebase.firestore
获取任务(Task<DocumentSnapshot>?
),而deleteFiles(it.media)
检索Firebase.storage
删除任务(Task<Void>?
).如果出现以下情况,应调用doSomething()
:删除任务成功或根本不需要删除(deleteFiles(it.media)
将返回null).
Where getItem(id)
returns the Firebase.firestore
get task (Task<DocumentSnapshot>?
) and deleteFiles(it.media)
retuens the Firebase.storage
delete task (Task<Void>?
). doSomething()
should be called if either: the delete task succeed or no delete was needed at all (deleteFiles(it.media)
would return null).
问题是当get任务失败时:在那种情况下,我希望deleteTask
是一个非null的任务(因为null对于我来说是一个有效的情况,如所解释的),并带有isSuccessful = false
(所以if (task.isSuccessful) ... else task
),但它给出了类型不匹配"信息.错误(由于Task<DocumentSnapshot>?
和Task<Void>?
是不同的类型,这很有意义...).
The problem is when the get task fails: In that case, I would like the deleteTask
to be a non-null Task (as null is a valid case for me as explained) with isSuccessful = false
(so doSomething()
won't be called), but I couldn't find a way to create a dummy failed task. I thought about returning the get task (if (task.isSuccessful) ... else task
), but it gives a "Type mismatch" error (which makes sense as Task<DocumentSnapshot>?
and Task<Void>?
are different types...).
那么,如何返回失败的任务导致继续执行任务?
So, How can I return a failed task result in a continuation task?
(有关为什么我需要这样做的更多信息:此处我用更多的细节解释了我的具体情况,但是据我从那里得到的答案了解,这些细节只会分散问题的注意力...)
(For more information on why I need this: here I explained my specific case with more details, but as I understood from the answer I got there, the details only distract from the question...)
推荐答案
好,我刚刚找到了想要的东西:
Tasks API具有Tasks
对象,该对象具有以下功能:forCanceled()
& forResult()
.在我仅调查Task
类并且不了解Tasks
对象之前...有关更多信息,请参见
Ok, I just found what I've been looking for:
Tasks API has a Tasks
object which has functions: forCanceled()
& forResult()
. Before I only investigated the Task
class and didn't know of the Tasks
object... For more information, see here.
这篇关于如何返回失败的任务导致继续执行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!