@@ -119,9 +119,11 @@ protected JSONObject generateJsonDiff(JSONObject cur, JSONObject changedTo, JSON
119
119
}
120
120
}
121
121
122
- protected synchronized UserState getToSyncUserState () {
123
- if (toSyncUserState == null )
124
- toSyncUserState = newUserState ("TOSYNC_STATE" , true );
122
+ protected UserState getToSyncUserState () {
123
+ synchronized (syncLock ) {
124
+ if (toSyncUserState == null )
125
+ toSyncUserState = newUserState ("TOSYNC_STATE" , true );
126
+ }
125
127
126
128
return toSyncUserState ;
127
129
}
@@ -163,7 +165,7 @@ private boolean syncEmailLogout() {
163
165
return getToSyncUserState ().dependValues .optBoolean ("logoutEmail" , false );
164
166
}
165
167
166
- synchronized void syncUserState (boolean fromSyncService ) {
168
+ void syncUserState (boolean fromSyncService ) {
167
169
runningSyncUserState .set (true );
168
170
internalSyncUserState (fromSyncService );
169
171
runningSyncUserState .set (false );
@@ -287,10 +289,12 @@ private void doPutSync(String userId, final JSONObject jsonBody, final JSONObjec
287
289
void onFailure (int statusCode , String response , Throwable throwable ) {
288
290
OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "Failed last request. statusCode: " + statusCode + "\n response: " + response );
289
291
290
- if (response400WithErrorsContaining (statusCode , response , "No user with this id found" ))
291
- handlePlayerDeletedFromServer ();
292
- else
293
- handleNetworkFailure ();
292
+ synchronized (syncLock ) {
293
+ if (response400WithErrorsContaining (statusCode , response , "No user with this id found" ))
294
+ handlePlayerDeletedFromServer ();
295
+ else
296
+ handleNetworkFailure ();
297
+ }
294
298
295
299
if (jsonBody .has ("tags" ))
296
300
for (ChangeTagsUpdateHandler handler : tagsHandlers ) {
@@ -303,8 +307,11 @@ void onFailure(int statusCode, String response, Throwable throwable) {
303
307
304
308
@ Override
305
309
void onSuccess (String response ) {
306
- currentUserState .persistStateAfterSync (dependDiff , jsonBody );
307
- onSuccessfulSync (jsonBody );
310
+ synchronized (syncLock ) {
311
+ currentUserState .persistStateAfterSync (dependDiff , jsonBody );
312
+ onSuccessfulSync (jsonBody );
313
+ }
314
+
308
315
JSONObject tags = OneSignalStateSynchronizer .getTags (false ).result ;
309
316
310
317
if (jsonBody .has ("tags" ) && tags != null )
@@ -330,36 +337,40 @@ private void doCreateOrNewSession(final String userId, final JSONObject jsonBody
330
337
OneSignalRestClient .postSync (urlStr , jsonBody , new OneSignalRestClient .ResponseHandler () {
331
338
@ Override
332
339
void onFailure (int statusCode , String response , Throwable throwable ) {
333
- waitingForSessionResponse = false ;
334
- OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "Failed last request. statusCode: " + statusCode + "\n response: " + response );
340
+ synchronized (syncLock ) {
341
+ waitingForSessionResponse = false ;
342
+ OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "Failed last request. statusCode: " + statusCode + "\n response: " + response );
335
343
336
- if (response400WithErrorsContaining (statusCode , response , "not a valid device_type" ))
337
- handlePlayerDeletedFromServer ();
338
- else
339
- handleNetworkFailure ();
344
+ if (response400WithErrorsContaining (statusCode , response , "not a valid device_type" ))
345
+ handlePlayerDeletedFromServer ();
346
+ else
347
+ handleNetworkFailure ();
348
+ }
340
349
}
341
350
342
351
@ Override
343
352
void onSuccess (String response ) {
344
- nextSyncIsSession = waitingForSessionResponse = false ;
345
- currentUserState .persistStateAfterSync (dependDiff , jsonBody );
353
+ synchronized (syncLock ) {
354
+ nextSyncIsSession = waitingForSessionResponse = false ;
355
+ currentUserState .persistStateAfterSync (dependDiff , jsonBody );
346
356
347
- try {
348
- JSONObject jsonResponse = new JSONObject (response );
357
+ try {
358
+ JSONObject jsonResponse = new JSONObject (response );
349
359
350
- if (jsonResponse .has ("id" )) {
351
- String newUserId = jsonResponse .optString ("id" );
352
- updateIdDependents (newUserId );
360
+ if (jsonResponse .has ("id" )) {
361
+ String newUserId = jsonResponse .optString ("id" );
362
+ updateIdDependents (newUserId );
353
363
354
- OneSignal .Log (OneSignal .LOG_LEVEL .INFO , "Device registered, UserId = " + newUserId );
355
- }
356
- else
357
- OneSignal .Log (OneSignal .LOG_LEVEL .INFO , "session sent, UserId = " + userId );
364
+ OneSignal .Log (OneSignal .LOG_LEVEL .INFO , "Device registered, UserId = " + newUserId );
365
+ }
366
+ else
367
+ OneSignal .Log (OneSignal .LOG_LEVEL .INFO , "session sent, UserId = " + userId );
358
368
359
- OneSignal .updateOnSessionDependents ();
360
- onSuccessfulSync (jsonBody );
361
- } catch (Throwable t ) {
362
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "ERROR parsing on_session or create JSON Response." , t );
369
+ OneSignal .updateOnSessionDependents ();
370
+ onSuccessfulSync (jsonBody );
371
+ } catch (Throwable t ) {
372
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "ERROR parsing on_session or create JSON Response." , t );
373
+ }
363
374
}
364
375
}
365
376
});
0 commit comments