Created
August 28, 2014 20:49
-
-
Save snadjafi/574c2342e3b4452c27c8 to your computer and use it in GitHub Desktop.
retrofit cancelableCallbacks for fragment use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CancelableCallback<T> implements Callback<T> { | |
private Callback<T> callback; | |
private boolean canceled; | |
public CancelableCallback(Callback<T> callback) { | |
this.callback = callback; | |
canceled = false; | |
} | |
public void cancel() { | |
canceled = true; | |
callback = null; | |
} | |
@Override public void success(T t, Response response) { | |
if (!canceled) { | |
callback.success(t,response); | |
} | |
} | |
@Override public void failure(RetrofitError error) { | |
if (!canceled) { | |
callback.failure(error); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Example extends Fragment { | |
private CancelableCallback<Model> mCallback = new CancelableCallback<Visit>(new Callback<Model>() { | |
@Override public void success(Model visit, Response response) { | |
} | |
@Override public void failure(RetrofitError error) { | |
}); | |
@Override public void onDestroy() { | |
super.onDestroy(); | |
mCallback.cancel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment