0

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()
...

Where getItem(id) returns the Firebase.firestore get task (Task<DocumentSnapshot>?) and deleteFiles(it.media) returns 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).

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...)

Elementary
  • 2,153
  • 2
  • 24
  • 35
Omer Levy
  • 347
  • 4
  • 11

1 Answers1

0

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.

Omer Levy
  • 347
  • 4
  • 11