@@ -247,28 +247,32 @@ public void remove(String key) {
247
247
return super .toRest (state , cleanOperationSetQueue , objectEncoder );
248
248
}
249
249
250
- /* package for tests */ void cleanUpAuthData () {
250
+ /* package for tests */ Task < Void > cleanUpAuthDataAsync () {
251
251
ParseAuthenticationManager controller = getAuthenticationManager ();
252
+ Map <String , Map <String , String >> authData ;
252
253
synchronized (mutex ) {
253
- Map < String , Map < String , String >> authData = getState ().authData ();
254
+ authData = getState ().authData ();
254
255
if (authData .size () == 0 ) {
255
- return ; // Nothing to see or do here...
256
+ return Task . forResult ( null ) ; // Nothing to see or do here...
256
257
}
258
+ }
257
259
258
- Iterator < Map . Entry < String , Map < String , String >>> i = authData . entrySet (). iterator ();
259
- while ( i . hasNext ()) {
260
- Map . Entry < String , Map < String , String >> entry = i . next ();
261
- if ( entry . getValue () == null ) {
262
- i . remove ();
263
- controller . restoreAuthentication ( entry . getKey (), null );
264
- }
260
+ List < Task < Void >> tasks = new ArrayList <> ();
261
+ Iterator < Map . Entry < String , Map < String , String >>> i = authData . entrySet (). iterator ();
262
+ while ( i . hasNext ()) {
263
+ Map . Entry < String , Map < String , String >> entry = i . next ();
264
+ if ( entry . getValue () == null ) {
265
+ i . remove ( );
266
+ tasks . add ( controller . restoreAuthenticationAsync ( entry . getKey (), null ). makeVoid ());
265
267
}
266
-
267
- State newState = getState ().newBuilder ()
268
- .authData (authData )
269
- .build ();
270
- setState (newState );
271
268
}
269
+
270
+ State newState = getState ().newBuilder ()
271
+ .authData (authData )
272
+ .build ();
273
+ setState (newState );
274
+
275
+ return Task .whenAll (tasks );
272
276
}
273
277
274
278
@ Override
@@ -488,17 +492,22 @@ private void restoreAnonymity(Map<String, String> anonymousData) {
488
492
task = super .saveAsync (sessionToken , toAwait );
489
493
}
490
494
491
- return task .onSuccessTask (new Continuation <Void , Task <Void >>() {
492
- @ Override
493
- public Task <Void > then (Task <Void > task ) throws Exception {
494
- // If the user is the currently logged in user, we persist all data to disk
495
- if (isCurrentUser ()) {
496
- cleanUpAuthData ();
495
+ if (isCurrentUser ()) {
496
+ // If the user is the currently logged in user, we persist all data to disk
497
+ return task .onSuccessTask (new Continuation <Void , Task <Void >>() {
498
+ @ Override
499
+ public Task <Void > then (Task <Void > task ) throws Exception {
500
+ return cleanUpAuthDataAsync ();
501
+ }
502
+ }).onSuccessTask (new Continuation <Void , Task <Void >>() {
503
+ @ Override
504
+ public Task <Void > then (Task <Void > task ) throws Exception {
497
505
return saveCurrentUserAsync (ParseUser .this );
498
506
}
499
- return Task .forResult (null );
500
- }
501
- });
507
+ });
508
+ }
509
+
510
+ return task ;
502
511
}
503
512
504
513
@ Override
@@ -531,29 +540,34 @@ public ParseUser fetch() throws ParseException {
531
540
@ Override
532
541
/* package */ <T extends ParseObject > Task <T > fetchAsync (
533
542
String sessionToken , Task <Void > toAwait ) {
534
- synchronized (mutex ) {
535
- //TODO (grantland): It doesn't seem like we should do this.. Why don't we error like we do
536
- // when fetching an unsaved ParseObject?
537
- if (isLazy ()) {
538
- return Task .forResult ((T ) this );
539
- }
543
+ //TODO (grantland): It doesn't seem like we should do this.. Why don't we error like we do
544
+ // when fetching an unsaved ParseObject?
545
+ if (isLazy ()) {
546
+ return Task .forResult ((T ) this );
547
+ }
540
548
541
- return super .<T > fetchAsync (sessionToken , toAwait ).onSuccessTask (new Continuation <T , Task <T >>() {
549
+ Task <T > task = super .fetchAsync (sessionToken , toAwait );
550
+
551
+ if (isCurrentUser ()) {
552
+ return task .onSuccessTask (new Continuation <T , Task <Void >>() {
542
553
@ Override
543
- public Task <T > then (final Task <T > fetchAsyncTask ) throws Exception {
544
- if (isCurrentUser ()) {
545
- cleanUpAuthData ();
546
- return saveCurrentUserAsync (ParseUser .this ).continueWithTask (new Continuation <Void , Task <T >>() {
547
- @ Override
548
- public Task <T > then (Task <Void > task ) throws Exception {
549
- return fetchAsyncTask ;
550
- }
551
- });
552
- }
553
- return fetchAsyncTask ;
554
+ public Task <Void > then (final Task <T > fetchAsyncTask ) throws Exception {
555
+ return cleanUpAuthDataAsync ();
556
+ }
557
+ }).onSuccessTask (new Continuation <Void , Task <Void >>() {
558
+ @ Override
559
+ public Task <Void > then (Task <Void > task ) throws Exception {
560
+ return saveCurrentUserAsync (ParseUser .this );
561
+ }
562
+ }).onSuccess (new Continuation <Void , T >() {
563
+ @ Override
564
+ public T then (Task <Void > task ) throws Exception {
565
+ return (T ) ParseUser .this ;
554
566
}
555
567
});
556
568
}
569
+
570
+ return task ;
557
571
}
558
572
559
573
/**
@@ -676,7 +690,8 @@ public Task<Void> then(Task<Void> task) throws Exception {
676
690
@ Override
677
691
public Task <Void > then (final Task <ParseUser .State > signUpTask ) throws Exception {
678
692
ParseUser .State result = signUpTask .getResult ();
679
- return handleSaveResultAsync (result , operations ).continueWithTask (new Continuation <Void , Task <Void >>() {
693
+ return handleSaveResultAsync (result ,
694
+ operations ).continueWithTask (new Continuation <Void , Task <Void >>() {
680
695
@ Override
681
696
public Task <Void > then (Task <Void > task ) throws Exception {
682
697
if (!signUpTask .isCancelled () && !signUpTask .isFaulted ()) {
@@ -1068,29 +1083,48 @@ public ParseUser fetchIfNeeded() throws ParseException {
1068
1083
return authData .containsKey (authType ) && authData .get (authType ) != null ;
1069
1084
}
1070
1085
1071
- /* package */ void synchronizeAuthData (String authType ) {
1086
+ /**
1087
+ * Ensures that all auth providers have auth data (e.g. access tokens, etc.) that matches this
1088
+ * user.
1089
+ */
1090
+ /* package */ Task <Void > synchronizeAllAuthDataAsync () {
1091
+ Map <String , Map <String , String >> authData ;
1072
1092
synchronized (mutex ) {
1073
1093
if (!isCurrentUser ()) {
1074
- return ;
1075
- }
1076
- boolean success = getAuthenticationManager ()
1077
- .restoreAuthentication (authType , getAuthData (authType ));
1078
- if (!success ) {
1079
- unlinkFromAsync (authType );
1094
+ return Task .forResult (null );
1080
1095
}
1096
+ authData = getAuthData ();
1081
1097
}
1098
+ List <Task <Void >> tasks = new ArrayList <>(authData .size ());
1099
+ for (String authType : authData .keySet ()) {
1100
+ tasks .add (synchronizeAuthDataAsync (authType ));
1101
+ }
1102
+ return Task .whenAll (tasks );
1082
1103
}
1083
1104
1084
- /**
1085
- * Ensures that all auth providers have auth data (e.g. access tokens, etc.) that matches this
1086
- * user.
1087
- */
1088
- /* package */ void synchronizeAllAuthData () {
1105
+ /* package */ Task <Void > synchronizeAuthDataAsync (String authType ) {
1106
+ Map <String , String > authData ;
1089
1107
synchronized (mutex ) {
1090
- for ( Map . Entry < String , Map < String , String >> entry : getAuthData (). entrySet ()) {
1091
- synchronizeAuthData ( entry . getKey () );
1108
+ if (! isCurrentUser ()) {
1109
+ return Task . forResult ( null );
1092
1110
}
1111
+ authData = getAuthData (authType );
1093
1112
}
1113
+ return synchronizeAuthDataAsync (getAuthenticationManager (), authType , authData );
1114
+ }
1115
+
1116
+ private Task <Void > synchronizeAuthDataAsync (
1117
+ ParseAuthenticationManager manager , final String authType , Map <String , String > authData ) {
1118
+ return manager .restoreAuthenticationAsync (authType , authData ).onSuccessTask (new Continuation <Boolean , Task <Void >>() {
1119
+ @ Override
1120
+ public Task <Void > then (Task <Boolean > task ) throws Exception {
1121
+ boolean success = task .getResult ();
1122
+ if (!success ) {
1123
+ return unlinkFromAsync (authType );
1124
+ }
1125
+ return task .makeVoid ();
1126
+ }
1127
+ });
1094
1128
}
1095
1129
1096
1130
/* package */ Task <Void > unlinkFromAsync (final String authType ) {
@@ -1236,8 +1270,7 @@ public Task<Void> then(Task<Void> task) throws Exception {
1236
1270
restoreAnonymity (oldAnonymousData );
1237
1271
return task ;
1238
1272
}
1239
- synchronizeAuthData (authType );
1240
- return task ;
1273
+ return synchronizeAuthDataAsync (authType );
1241
1274
}
1242
1275
}
1243
1276
});
0 commit comments