Created
February 15, 2012 02:16
-
-
Save shawnoster/1832571 to your computer and use it in GitHub Desktop.
AgFx pattern to verify you have valid data (either from cache or web)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void AuthenticateUser(Action<AuthorizedUser> completed, Action<Exception> error) | |
{ | |
EnsureIsAuthenticated(); | |
AuthorizedUser = DataManager.Current.LoadFromCache<AuthorizedUser>("authenticated_user"); | |
// LoadFromCache returns an object even if it didn't exist in the | |
// cache so check one of its properties for existance vs. the object | |
// itself. | |
if (!String.IsNullOrEmpty(AuthorizedUser.Id)) | |
{ | |
if (completed != null) | |
completed(AuthorizedUser); | |
return; | |
} | |
// Force a network request of the AuthorizedUser because the code | |
// above has just confirmed it doesn't exist in the cache. This | |
// funky deconstruction of what a normal Load<>() does is so the | |
// calling code can wait on getting a "real" instance vs. an | |
// empty one that then pops with live data. | |
DataManager.Current.Refresh<AuthorizedUser>( | |
"authenticated_user", | |
user => | |
{ | |
this.AuthorizedUser = user; | |
if (completed != null) | |
completed(AuthorizedUser); | |
}, | |
e => | |
{ | |
if (error != null) | |
{ | |
error(e); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment