Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSuccessTask vs addOnSuccessListener?

I'm using Google Firestore for by Android database and want to know the different between onSuccessTask and addOnSuccessListener.

For example, here is me updating a Firestore document:

val doc = db.collection("books").document(book).update(data)

For the above, to take action when the update completes, I can do either:

.onSuccessTask { } or .addOnSuccessListener { }

which to me, yields the exact same result.

Can someone clear up what the difference is and which one should be used?

like image 846
Zorgan Avatar asked Nov 03 '25 17:11

Zorgan


2 Answers

There are three flavours of Task's addOnSuccessListener() method which are:

  • addOnSuccessListener(Executor executor, OnSuccessListener listener):

Adds a listener that is called if the Task completes successfully.

  • addOnSuccessListener(OnSuccessListener listener):

Adds a listener that is called if the Task completes successfully.

  • addOnSuccessListener(Activity activity, OnSuccessListener listener):

Adds an Activity-scoped listener that is called if the Task completes successfully.

And two flavours of Task's onSuccessTask() method whic are:

  • onSuccessTask(Executor executor, SuccessContinuation successContinuation):

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

  • onSuccessTask(SuccessContinuation successContinuation):

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

As you can probably see, the main difference is that, in case of addOnSuccessListener() the object that is returned is of type abstract Task<TResult>, so everytime you use it, you'll need to provide an implementation for that while when using onSuccessTask() method, the type of object that is returned is <TContinuationResult> Task<TContinuationResult> (which is not abstract).

like image 151
Alex Mamo Avatar answered Nov 05 '25 15:11

Alex Mamo


Basically, you would use addOnSuccessListener when you just want to work with the result, while onSuccessTask can be used with other methods such as continueWith or continueWithTask in order to chain tasks.

Here's an article about chaining tasks.

like image 25
Pat Lee Avatar answered Nov 05 '25 15:11

Pat Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!