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

execute calls to AsyncStorageModule serially #20386

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
Copy link
Contributor

@gengjiawen gengjiawen Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why touch this line ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android Studio was showing warning, so fixed it 😄

* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
Expand All @@ -8,14 +8,15 @@
package com.facebook.react.modules.storage;

import java.util.HashSet;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.GuardedAsyncTask;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -42,6 +43,7 @@ public final class AsyncStorageModule

private ReactDatabaseSupplier mReactDatabaseSupplier;
private boolean mShuttingDown = false;
private Executor executor = Executors.newSingleThreadExecutor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if adding a new executor is the best approach here, but If you are adding this you should override the onCatalystInstanceDestroy method and then shutdown the executor.
See:


public AsyncStorageModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -83,9 +85,9 @@ public void multiGet(final ReadableArray keys, final Callback callback) {
return;
}

new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if an exception is thrown when executing the Runnable?

GuardedAsyncTask provided error management, see: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedAsyncTask.java#L34

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use executeOnExecutor (https://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor,%20Params...)) instead of execute and keep using GuardedAsyncTask instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janicduplessis got it and ready to push the change, but waiting for CI to become green again.

@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null), null);
return;
Expand Down Expand Up @@ -141,7 +143,7 @@ protected void doInBackgroundGuarded(Void... params) {

callback.invoke(null, data);
}
}.execute();
});
}

/**
Expand All @@ -156,9 +158,9 @@ public void multiSet(final ReadableArray keyValueArray, final Callback callback)
return;
}

new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null));
return;
Expand Down Expand Up @@ -208,7 +210,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
});
}

/**
Expand All @@ -221,9 +223,9 @@ public void multiRemove(final ReadableArray keys, final Callback callback) {
return;
}

new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null));
return;
Expand Down Expand Up @@ -259,7 +261,7 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
});
}

/**
Expand All @@ -268,9 +270,9 @@ protected void doInBackgroundGuarded(Void... params) {
*/
@ReactMethod
public void multiMerge(final ReadableArray keyValueArray, final Callback callback) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null));
return;
Expand Down Expand Up @@ -322,17 +324,17 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke();
}
}
}.execute();
});
}

/**
* Clears the database.
*/
@ReactMethod
public void clear(final Callback callback) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!mReactDatabaseSupplier.ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null));
return;
Expand All @@ -345,17 +347,17 @@ protected void doInBackgroundGuarded(Void... params) {
callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()));
}
}
}.execute();
});
}

/**
* Returns an array with all keys from the database.
*/
@ReactMethod
public void getAllKeys(final Callback callback) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
execute(new Runnable() {
@Override
protected void doInBackgroundGuarded(Void... params) {
public void run() {
if (!ensureDatabase()) {
callback.invoke(AsyncStorageErrorUtil.getDBError(null), null);
return;
Expand All @@ -379,7 +381,7 @@ protected void doInBackgroundGuarded(Void... params) {
}
callback.invoke(null, data);
}
}.execute();
});
}

/**
Expand All @@ -388,4 +390,8 @@ protected void doInBackgroundGuarded(Void... params) {
private boolean ensureDatabase() {
return !mShuttingDown && mReactDatabaseSupplier.ensureDatabase();
}

private void execute(Runnable r) {
executor.execute(r);
}
}