Skip to content

Instantly share code, notes, and snippets.

@shawnoster
Created February 15, 2012 02:16
Show Gist options
  • Save shawnoster/1832571 to your computer and use it in GitHub Desktop.
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)
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