Search code examples
androidbolts-framework

What's the difference between continueWith() and onSuccess() in Bolts framework?


I am using Bolts framework in my Android project. I read the documents several times, but I am still confused about the difference between continueWith() and onSuccess(), because the callback method and the return value are all the same. For example,

Task task = ParseGeoPoint.getCurrentLocationInBackground(10*1000);

And what's the difference between these two methods?

task.onSuccess(new Continuation<ParseGeoPoint, Object>() {
    @Override
    public Object then(Task<ParseGeoPoint> task) throws Exception {
        Log.d(TAG, "task done");
        return null;
    }
});

task.continueWith(new Continuation<ParseGeoPoint, Object>() {
    @Override
    public Object then(Task<ParseGeoPoint> task) throws Exception {
        Log.d(TAG, "task done");
        return null;
    }
});

Solution

  • Basically, onSuccess() is just called, as its name indicates, when the call completes without errors. On the other hand, continueWith() is called always, even in case of failure. Therefore, use onSuccess() when you are interested only in retrieve the result on successful requests, and use continueWith() if you want also be able to handle failed requests.