@@ -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 () {
0 commit comments