Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to only cancel async calls without destroying CustomerSessions instance. #624

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions stripe/src/main/java/com/stripe/android/CustomerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class CustomerSession implements EphemeralKeyManager.KeyManagerListener {
// A queue of Runnables for doing customer updates
private final BlockingQueue<Runnable> mNetworkQueue = new LinkedBlockingQueue<>();

private @NonNull ThreadPoolExecutor mThreadPoolExecutor;
private final @NonNull ThreadPoolExecutor mThreadPoolExecutor;

private static final int CUSTOMER_RETRIEVED = 7;
private static final int CUSTOMER_ERROR = 11;
Expand Down Expand Up @@ -143,11 +143,23 @@ static void initCustomerSession(

@VisibleForTesting
static void clearInstance() {
cancelCallbacks();
mInstance = null;
}

/**
* End any async calls in process and will not invoke callback listeners.
* It will not clear the singleton instance of a {@link CustomerSession} so it can be
* safely used when a view is being removed/destroyed to avoid null pointer exceptions
* due to async operation delay.
* No need to call {@link CustomerSession#initCustomerSession(EphemeralKeyProvider)} again
* after this operation.
*/
public static void cancelCallbacks() {
if (mInstance == null) {
return;
}
mInstance.mThreadPoolExecutor.shutdownNow();
mInstance = null;
}

private CustomerSession(
Expand Down