@@ -247,28 +247,32 @@ public void remove(String key) {
247247 return super .toRest (state , cleanOperationSetQueue , objectEncoder );
248248 }
249249
250- /* package for tests */ void cleanUpAuthData () {
250+ /* package for tests */ Task < Void > cleanUpAuthDataAsync () {
251251 ParseAuthenticationManager controller = getAuthenticationManager ();
252+ Map <String , Map <String , String >> authData ;
252253 synchronized (mutex ) {
253- Map < String , Map < String , String >> authData = getState ().authData ();
254+ authData = getState ().authData ();
254255 if (authData .size () == 0 ) {
255- return ; // Nothing to see or do here...
256+ return Task . forResult ( null ) ; // Nothing to see or do here...
256257 }
258+ }
257259
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 ());
265267 }
266-
267- State newState = getState ().newBuilder ()
268- .authData (authData )
269- .build ();
270- setState (newState );
271268 }
269+
270+ State newState = getState ().newBuilder ()
271+ .authData (authData )
272+ .build ();
273+ setState (newState );
274+
275+ return Task .whenAll (tasks );
272276 }
273277
274278 @ Override
@@ -488,17 +492,22 @@ private void restoreAnonymity(Map<String, String> anonymousData) {
488492 task = super .saveAsync (sessionToken , toAwait );
489493 }
490494
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 {
497505 return saveCurrentUserAsync (ParseUser .this );
498506 }
499- return Task .forResult (null );
500- }
501- });
507+ });
508+ }
509+
510+ return task ;
502511 }
503512
504513 @ Override
@@ -531,29 +540,34 @@ public ParseUser fetch() throws ParseException {
531540 @ Override
532541 /* package */ <T extends ParseObject > Task <T > fetchAsync (
533542 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+ }
540548
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 >>() {
542553 @ 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 ;
554566 }
555567 });
556568 }
569+
570+ return task ;
557571 }
558572
559573 /**
@@ -676,7 +690,8 @@ public Task<Void> then(Task<Void> task) throws Exception {
676690 @ Override
677691 public Task <Void > then (final Task <ParseUser .State > signUpTask ) throws Exception {
678692 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 >>() {
680695 @ Override
681696 public Task <Void > then (Task <Void > task ) throws Exception {
682697 if (!signUpTask .isCancelled () && !signUpTask .isFaulted ()) {
@@ -1068,29 +1083,48 @@ public ParseUser fetchIfNeeded() throws ParseException {
10681083 return authData .containsKey (authType ) && authData .get (authType ) != null ;
10691084 }
10701085
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 ;
10721092 synchronized (mutex ) {
10731093 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 );
10801095 }
1096+ authData = getAuthData ();
10811097 }
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 );
10821103 }
10831104
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 ;
10891107 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 );
10921110 }
1111+ authData = getAuthData (authType );
10931112 }
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+ });
10941128 }
10951129
10961130 /* package */ Task <Void > unlinkFromAsync (final String authType ) {
@@ -1236,8 +1270,7 @@ public Task<Void> then(Task<Void> task) throws Exception {
12361270 restoreAnonymity (oldAnonymousData );
12371271 return task ;
12381272 }
1239- synchronizeAuthData (authType );
1240- return task ;
1273+ return synchronizeAuthDataAsync (authType );
12411274 }
12421275 }
12431276 });
0 commit comments