Skip to content

Commit bef8671

Browse files
authored
fix: Clean up internal state on reload (#16)
1 parent 33c6840 commit bef8671

File tree

9 files changed

+45
-5
lines changed

9 files changed

+45
-5
lines changed

android/cpp-adapter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ struct QuickSQLiteBridge : jni::JavaClass<QuickSQLiteBridge> {
2828

2929
osp::install(*jsiRuntime, jsCallInvoker, docPathString.c_str());
3030
}
31+
32+
static void clearStateNativeJsi() {
33+
osp::clearState();
34+
}
3135
};
3236

3337
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
3438
return jni::initialize(vm, [] { QuickSQLiteBridge::registerNatives(); });
35-
}
39+
}

android/src/main/java/com/reactnativequicksqlite/QuickSQLiteBridge.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class QuickSQLiteBridge {
1010
private native void installNativeJsi(long jsContextNativePointer, CallInvokerHolderImpl jsCallInvokerHolder, String docPath);
11+
private native void clearStateNativeJsi();
1112
public static final QuickSQLiteBridge instance = new QuickSQLiteBridge();
1213

1314
public void install(ReactContext context) {
@@ -21,4 +22,8 @@ public void install(ReactContext context) {
2122
path
2223
);
2324
}
24-
}
25+
26+
public void clearState() {
27+
clearStateNativeJsi();
28+
}
29+
}

android/src/main/java/com/reactnativequicksqlite/SequelModule.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public boolean install() {
3434
return false;
3535
}
3636
}
37-
}
37+
38+
@Override
39+
public void onCatalystInstanceDestroy() {
40+
try {
41+
QuickSQLiteBridge.instance.clearState();
42+
} catch (Exception exception) {
43+
Log.e(NAME, "Failed to clear state!", exception);
44+
}
45+
}
46+
}

cpp/bindings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ namespace osp {
1717
string docPathStr;
1818
std::shared_ptr<react::CallInvoker> invoker;
1919

20+
void clearState() {
21+
sqliteCloseAll();
22+
}
23+
2024
void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker, const char *docPath)
2125
{
2226
docPathStr = std::string(docPath);

cpp/bindings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ using namespace facebook;
66

77
namespace osp {
88
void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker, const char *docPath);
9+
void clearState();
910
}
1011

cpp/sqliteBridge.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ SQLiteOPResult sqliteCloseDb(string const dbName)
121121

122122
sqlite3 *db = dbMap[dbName];
123123

124-
sqlite3_close(db);
124+
sqlite3_close_v2(db);
125125

126126
dbMap.erase(dbName);
127127

@@ -130,6 +130,15 @@ SQLiteOPResult sqliteCloseDb(string const dbName)
130130
};
131131
}
132132

133+
void sqliteCloseAll() {
134+
for (auto const& x : dbMap) {
135+
// In certain cases, this will return SQLITE_OK, mark the database connection as an unusable "zombie",
136+
// and deallocate the connection later.
137+
sqlite3_close_v2(x.second);
138+
}
139+
dbMap.clear();
140+
}
141+
133142
SQLiteOPResult sqliteAttachDb(string const mainDBName, string const docPath, string const databaseToAttach, string const alias)
134143
{
135144
/**

cpp/sqliteBridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ SQLiteOPResult sqliteDetachDb(string const mainDBName, string const alias);
2626
SQLiteOPResult sqliteExecute(string const dbName, string const &query, vector<QuickValue> *values, vector<map<string, QuickValue>> *result, vector<QuickColumnMetadata> *metadata);
2727

2828
SequelLiteralUpdateResult sqliteExecuteLiteral(string const dbName, string const &query);
29+
30+
void sqliteCloseAll();

ios/QuickSQLite.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <React/RCTBridgeModule.h>
2+
#import <React/RCTInvalidating.h>
23

3-
@interface QuickSQLite : NSObject <RCTBridgeModule>
4+
@interface QuickSQLite : NSObject <RCTBridgeModule, RCTInvalidating>
45

56
@property(nonatomic, assign) BOOL setBridgeOnMainQueue;
67

ios/QuickSQLite.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ @implementation QuickSQLite
5757
return @true;
5858
}
5959

60+
- (void)invalidate {
61+
osp::clearState();
62+
}
63+
64+
6065
@end

0 commit comments

Comments
 (0)