Skip to content

Commit f9e58a0

Browse files
committed
[android] Make sure to have only one instance of CodePush
1 parent 563dbad commit f9e58a0

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

android/app/src/main/java/com/appzung/codepush/react/CodePush.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
import java.util.List;
2020

2121
public class CodePush implements ReactPackage {
22+
private static final Object LOCK = new Object();
23+
private static volatile CodePush mCurrentInstance;
24+
25+
public static CodePush getInstance(Context context) {
26+
if (mCurrentInstance == null) {
27+
synchronized (LOCK) {
28+
if (mCurrentInstance == null) {
29+
mCurrentInstance = new CodePush(context);
30+
}
31+
}
32+
}
33+
return mCurrentInstance;
34+
}
2235

2336
private static boolean sIsRunningBinaryVersion = false;
2437
private static boolean sNeedToReportRollback = false;
@@ -47,13 +60,11 @@ public class CodePush implements ReactPackage {
4760

4861
private static ReactHostHolder mReactHostHolder;
4962

50-
private static CodePush mCurrentInstance;
51-
5263
public static String getServiceUrl() {
5364
return mServerUrl;
5465
}
5566

56-
public CodePush(Context context) {
67+
private CodePush(Context context) {
5768
mContext = context.getApplicationContext();
5869

5970
String releaseChannelPublicIdFromStrings = getCustomPropertyFromStringsIfExist("ReleaseChannelPublicId");

react-native.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
dependency: {
33
platforms: {
44
android: {
5-
packageInstance: 'new CodePush(getApplicationContext())',
5+
packageInstance: 'CodePush.getInstance(getApplicationContext())',
66
sourceDir: './android/app',
77
},
88
},

0 commit comments

Comments
 (0)