Skip to content

Commit e8e4f7b

Browse files
committed
perf(android): Run query in background thread
This change would avoid blocking UI thread
1 parent 8216cb6 commit e8e4f7b

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

android/src/main/java/dog/craftz/sqlite_2/RNSqlite2Module.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,40 +76,45 @@ public void test(String message, Promise promise) {
7676
}
7777

7878
@ReactMethod
79-
public void exec(String dbName, ReadableArray queries, Boolean readOnly, Promise promise) {
80-
Log.d("example", "test called: " + dbName);
79+
public void exec(final String dbName, final ReadableArray queries, final Boolean readOnly, final Promise promise) {
80+
debug("test called: %s", dbName);
8181

82-
try {
83-
int numQueries = queries.size();
84-
SQLitePLuginResult[] results = new SQLitePLuginResult[numQueries];
85-
SQLiteDatabase db = getDatabase(dbName);
86-
87-
for (int i = 0; i < numQueries; i++) {
88-
ReadableArray sqlQuery = queries.getArray(i);
89-
String sql = sqlQuery.getString(0);
90-
String[] bindArgs = convertParamsToStringArray(sqlQuery.getArray(1));
82+
backgroundHandler.post(new Runnable() {
83+
@Override
84+
public void run() {
9185
try {
92-
if (isSelect(sql)) {
93-
results[i] = doSelectInBackgroundAndPossiblyThrow(sql, bindArgs, db);
94-
} else { // update/insert/delete
95-
if (readOnly) {
96-
results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, new ReadOnlyException());
97-
} else {
98-
results[i] = doUpdateInBackgroundAndPossiblyThrow(sql, bindArgs, db);
86+
int numQueries = queries.size();
87+
SQLitePLuginResult[] results = new SQLitePLuginResult[numQueries];
88+
SQLiteDatabase db = getDatabase(dbName);
89+
90+
for (int i = 0; i < numQueries; i++) {
91+
ReadableArray sqlQuery = queries.getArray(i);
92+
String sql = sqlQuery.getString(0);
93+
String[] bindArgs = convertParamsToStringArray(sqlQuery.getArray(1));
94+
try {
95+
if (isSelect(sql)) {
96+
results[i] = doSelectInBackgroundAndPossiblyThrow(sql, bindArgs, db);
97+
} else { // update/insert/delete
98+
if (readOnly) {
99+
results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, new ReadOnlyException());
100+
} else {
101+
results[i] = doUpdateInBackgroundAndPossiblyThrow(sql, bindArgs, db);
102+
}
103+
}
104+
} catch (Throwable e) {
105+
if (DEBUG_MODE) {
106+
e.printStackTrace();
107+
}
108+
results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, e);
99109
}
100110
}
101-
} catch (Throwable e) {
102-
if (DEBUG_MODE) {
103-
e.printStackTrace();
104-
}
105-
results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, e);
111+
NativeArray data = pluginResultsToPrimitiveData(results);
112+
promise.resolve(data);
113+
} catch (Exception e) {
114+
promise.reject("SQLiteError", e);
106115
}
107116
}
108-
NativeArray data = pluginResultsToPrimitiveData(results);
109-
promise.resolve(data);
110-
} catch (Exception e) {
111-
promise.reject("SQLiteError", e);
112-
}
117+
});
113118
}
114119

115120
// do a update/delete/insert operation

0 commit comments

Comments
 (0)