Skip to content

Commit

Permalink
[Android]: Database locale issues (#423)
Browse files Browse the repository at this point in the history
Make sure that we close db connection when 'host' is destroyed and re-create it when needed
  • Loading branch information
krizzu committed Sep 30, 2020
1 parent b1b5bcf commit 9823e89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.GuardedAsyncTask;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -33,7 +34,7 @@

@ReactModule(name = AsyncStorageModule.NAME)
public final class AsyncStorageModule
extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable {
extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable, LifecycleEventListener {

// changed name to not conflict with AsyncStorage from RN repo
public static final String NAME = "RNC_AsyncSQLiteDBStorage";
Expand Down Expand Up @@ -91,6 +92,7 @@ public AsyncStorageModule(ReactApplicationContext reactContext) {
AsyncStorageModule(ReactApplicationContext reactContext, Executor executor) {
super(reactContext);
this.executor = new SerialExecutor(executor);
reactContext.addLifecycleEventListener(this);
mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext);
}

Expand Down Expand Up @@ -118,6 +120,18 @@ public void clearSensitiveData() {
mReactDatabaseSupplier.clearAndCloseDatabase();
}

@Override
public void onHostResume() {}

@Override
public void onHostPause() {}

@Override
public void onHostDestroy() {
// ensure we close database when activity is destroyed
mReactDatabaseSupplier.closeDatabase();
}

/**
* Given an array of keys, this returns a map of (key, value) pairs for the keys found, and
* (key, null) for the keys that haven't been found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

package com.reactnativecommunity.asyncstorage;

import javax.annotation.Nullable;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
import javax.annotation.Nullable;

/**
* Database supplier of the database used by react native. This creates, opens and deletes the
Expand Down Expand Up @@ -151,7 +149,7 @@ private synchronized boolean deleteDatabase() {
return mContext.deleteDatabase(DATABASE_NAME);
}

private synchronized void closeDatabase() {
public synchronized void closeDatabase() {
if (mDb != null && mDb.isOpen()) {
mDb.close();
mDb = null;
Expand Down

0 comments on commit 9823e89

Please sign in to comment.