mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-22 15:36:10 +08:00
Refactor UpdateContext to implement singleton pattern and ensure synchronous SharedPreferences writes. Update UpdateModule constructors to utilize the singleton instance.
This commit is contained in:
@@ -22,6 +22,10 @@ public class UpdateContext {
|
|||||||
public static boolean DEBUG = false;
|
public static boolean DEBUG = false;
|
||||||
private static ReactInstanceManager mReactInstanceManager;
|
private static ReactInstanceManager mReactInstanceManager;
|
||||||
private static boolean isUsingBundleUrl = false;
|
private static boolean isUsingBundleUrl = false;
|
||||||
|
|
||||||
|
// Singleton instance
|
||||||
|
private static UpdateContext sInstance;
|
||||||
|
private static final Object sLock = new Object();
|
||||||
|
|
||||||
public UpdateContext(Context context) {
|
public UpdateContext(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@@ -54,13 +58,17 @@ public class UpdateContext {
|
|||||||
boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
|
boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
|
||||||
|
|
||||||
if (packageVersionChanged || buildTimeChanged) {
|
if (packageVersionChanged || buildTimeChanged) {
|
||||||
|
// Execute cleanUp before clearing SharedPreferences to avoid race condition
|
||||||
|
this.cleanUp();
|
||||||
|
|
||||||
SharedPreferences.Editor editor = sp.edit();
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
editor.clear();
|
editor.clear();
|
||||||
editor.putString("packageVersion", packageVersion);
|
editor.putString("packageVersion", packageVersion);
|
||||||
editor.putString("buildTime", buildTime);
|
editor.putString("buildTime", buildTime);
|
||||||
editor.apply();
|
// Use commit() instead of apply() to ensure synchronous write completion
|
||||||
|
// This prevents race condition where getBundleUrl() might read null values
|
||||||
this.cleanUp();
|
// if called before apply() completes
|
||||||
|
editor.commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,12 +235,26 @@ public class UpdateContext {
|
|||||||
return mReactInstanceManager;
|
return mReactInstanceManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get singleton instance of UpdateContext
|
||||||
|
*/
|
||||||
|
public static UpdateContext getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
synchronized (sLock) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new UpdateContext(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public static String getBundleUrl(Context context) {
|
public static String getBundleUrl(Context context) {
|
||||||
return new UpdateContext(context.getApplicationContext()).getBundleUrl();
|
return getInstance(context).getBundleUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
|
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
|
||||||
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl);
|
return getInstance(context).getBundleUrl(defaultAssetsUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBundleUrl() {
|
public String getBundleUrl() {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class UpdateModule extends NativePushySpec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UpdateModule(ReactApplicationContext reactContext) {
|
public UpdateModule(ReactApplicationContext reactContext) {
|
||||||
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
|
this(reactContext, UpdateContext.getInstance(reactContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UpdateModule(ReactApplicationContext reactContext) {
|
public UpdateModule(ReactApplicationContext reactContext) {
|
||||||
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
|
this(reactContext, UpdateContext.getInstance(reactContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ export class UpdateContext {
|
|||||||
this.preferences.putSync('packageVersion', packageVersion);
|
this.preferences.putSync('packageVersion', packageVersion);
|
||||||
this.preferences.flush();
|
this.preferences.flush();
|
||||||
} else if (storedVersion && packageVersion !== storedVersion) {
|
} else if (storedVersion && packageVersion !== storedVersion) {
|
||||||
|
this.cleanUp();
|
||||||
this.preferences.clear();
|
this.preferences.clear();
|
||||||
this.preferences.putSync('packageVersion', packageVersion);
|
this.preferences.putSync('packageVersion', packageVersion);
|
||||||
this.preferences.flush();
|
this.preferences.flush();
|
||||||
this.cleanUp();
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to init preferences:', e);
|
console.error('Failed to init preferences:', e);
|
||||||
|
|||||||
Reference in New Issue
Block a user