我在我的Android项目中使用Bolts框架。我多次阅读了文档,但我仍然对 continueWith() 和 onSuccess() 之间的区别感到困惑,因为回调方法和返回值都是一样的。例如
Task task = ParseGeoPoint.getCurrentLocationInBackground(10*1000);
这两种方法有什么区别?
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;
}
});
基本上,onSuccess()
只是在调用完成时调用,顾名思义,当调用完成而没有错误时。另一方面,即使在发生故障的情况下,也始终调用continueWith()
。因此,当您只对检索成功请求的结果感兴趣时,请使用onSuccess()
;如果您还希望能够处理失败的请求,请使用continueWith()
。