Skip to content

Commit c713253

Browse files
committed
Fixed idsAvailable and init NPE when called from 2 threads
* If idsAvailable is called from another thread while OneSignal is being initialized it can results in an NPE. * Changed OneSignalStateSynchronizer backing state to load lazily to fix this. * Fixes issue #231
1 parent 356d098 commit c713253

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalStateSynchronizer.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ class OneSignalStateSynchronizer {
5151
// toSyncUserState - Pending state that will be synced to the OneSignal server.
5252
// diff will be generated between currentUserState when a sync call is made to the server.
5353
private static UserState currentUserState, toSyncUserState;
54+
55+
private static UserState getToSyncUserState() {
56+
synchronized (syncLock) {
57+
if (toSyncUserState == null)
58+
toSyncUserState = new OneSignalStateSynchronizer().new UserState("TOSYNC_STATE", true);
59+
}
60+
61+
return toSyncUserState;
62+
}
5463

5564
static HashMap<Integer, NetworkHandlerThread> networkHandlerThreads = new HashMap<>();
5665
private static final Object networkHandlerSyncLock = new Object() {};
@@ -217,11 +226,8 @@ static boolean stopAndPersist() {
217226
}
218227

219228
static void clearLocation() {
220-
if (toSyncUserState == null)
221-
return;
222-
223-
toSyncUserState.clearLocation();
224-
toSyncUserState.persistState();
229+
getToSyncUserState().clearLocation();
230+
getToSyncUserState().persistState();
225231
}
226232

227233
class UserState {
@@ -730,7 +736,7 @@ static void setSubscription(boolean enable) {
730736
}
731737

732738
static boolean getUserSubscribePreference() {
733-
return toSyncUserState.dependValues.optBoolean("userSubscribePref", true);
739+
return getToSyncUserState().dependValues.optBoolean("userSubscribePref", true);
734740
}
735741

736742
static void setPermission(boolean enable) {
@@ -747,12 +753,12 @@ static void updateLocation(LocationGMS.LocationPoint point) {
747753
}
748754

749755
static boolean getSubscribed() {
750-
return toSyncUserState.getNotificationTypes() > 0;
756+
return getToSyncUserState().getNotificationTypes() > 0;
751757
}
752758

753759

754760
static String getRegistrationId() {
755-
return toSyncUserState.syncValues.optString("identifier", null);
761+
return getToSyncUserState().syncValues.optString("identifier", null);
756762
}
757763

758764
static class GetTagsResult {
@@ -799,7 +805,7 @@ void onSuccess(String responseStr) {
799805
}
800806

801807
synchronized(syncLock) {
802-
return new GetTagsResult(serverSuccess, getTagsWithoutDeletedKeys(toSyncUserState.syncValues));
808+
return new GetTagsResult(serverSuccess, getTagsWithoutDeletedKeys(getToSyncUserState().syncValues));
803809
}
804810
}
805811

0 commit comments

Comments
 (0)