Skip to content

Commit dc9b5d7

Browse files
committed
Refactor UpdateContext to implement singleton pattern and ensure synchronous SharedPreferences writes. Update UpdateModule constructors to utilize the singleton instance.
1 parent e151c9c commit dc9b5d7

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

android/src/main/java/cn/reactnative/modules/update/UpdateContext.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public class UpdateContext {
2222
public static boolean DEBUG = false;
2323
private static ReactInstanceManager mReactInstanceManager;
2424
private static boolean isUsingBundleUrl = false;
25+
26+
// Singleton instance
27+
private static UpdateContext sInstance;
28+
private static final Object sLock = new Object();
2529

2630
public UpdateContext(Context context) {
2731
this.context = context;
@@ -54,13 +58,17 @@ public UpdateContext(Context context) {
5458
boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
5559

5660
if (packageVersionChanged || buildTimeChanged) {
61+
// Execute cleanUp before clearing SharedPreferences to avoid race condition
62+
this.cleanUp();
63+
5764
SharedPreferences.Editor editor = sp.edit();
5865
editor.clear();
5966
editor.putString("packageVersion", packageVersion);
6067
editor.putString("buildTime", buildTime);
61-
editor.apply();
62-
63-
this.cleanUp();
68+
// Use commit() instead of apply() to ensure synchronous write completion
69+
// This prevents race condition where getBundleUrl() might read null values
70+
// if called before apply() completes
71+
editor.commit();
6472
}
6573
}
6674

@@ -227,12 +235,26 @@ public ReactInstanceManager getCustomReactInstanceManager() {
227235
return mReactInstanceManager;
228236
}
229237

238+
/**
239+
* Get singleton instance of UpdateContext
240+
*/
241+
public static UpdateContext getInstance(Context context) {
242+
if (sInstance == null) {
243+
synchronized (sLock) {
244+
if (sInstance == null) {
245+
sInstance = new UpdateContext(context.getApplicationContext());
246+
}
247+
}
248+
}
249+
return sInstance;
250+
}
251+
230252
public static String getBundleUrl(Context context) {
231-
return new UpdateContext(context.getApplicationContext()).getBundleUrl();
253+
return getInstance(context).getBundleUrl();
232254
}
233255

234256
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
235-
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl);
257+
return getInstance(context).getBundleUrl(defaultAssetsUrl);
236258
}
237259

238260
public String getBundleUrl() {

android/src/newarch/cn/reactnative/modules/update/UpdateModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public UpdateModule(ReactApplicationContext reactContext, UpdateContext updateCo
2727
}
2828

2929
public UpdateModule(ReactApplicationContext reactContext) {
30-
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
30+
this(reactContext, UpdateContext.getInstance(reactContext));
3131
}
3232

3333
@Override

android/src/oldarch/cn/reactnative/modules/update/UpdateModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public UpdateModule(ReactApplicationContext reactContext, UpdateContext updateCo
4040
}
4141

4242
public UpdateModule(ReactApplicationContext reactContext) {
43-
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
43+
this(reactContext, UpdateContext.getInstance(reactContext));
4444
}
4545

4646
@Override

harmony/pushy/src/main/ets/UpdateContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export class UpdateContext {
3737
this.preferences.putSync('packageVersion', packageVersion);
3838
this.preferences.flush();
3939
} else if (storedVersion && packageVersion !== storedVersion) {
40+
this.cleanUp();
4041
this.preferences.clear();
4142
this.preferences.putSync('packageVersion', packageVersion);
4243
this.preferences.flush();
43-
this.cleanUp();
4444
}
4545
} catch (e) {
4646
console.error('Failed to init preferences:', e);

0 commit comments

Comments
 (0)