Skip to content

Instantly share code, notes, and snippets.

@ulyssesdotcodes
Last active August 29, 2015 14:13
Show Gist options
  • Save ulyssesdotcodes/76f63bff77763b29c1c6 to your computer and use it in GitHub Desktop.
Save ulyssesdotcodes/76f63bff77763b29c1c6 to your computer and use it in GitHub Desktop.
Data loading
We have many different types of data being loaded. Right now, we have Board and List<TrelloAction>, so we'll focus on those.
There are 2 possible data sources, the database and service.
For both of those, there are 3 possible states: NOT_LOADING, LOADING, and LOADED. Let's call this a LoadState enum.
This can be represented by a class TObjectLoadingState. It has three fields: LoadState mDbLoadState, LoadState mServiceLoadState, and TObject mData or List<TObject> mData.
BoardActivityData::State contains a TObjectLoadingState for each of the pieces of data being loaded.
public static class State {
final TObjectLoadingState<Board> mBoardState;
final TObjectListLoadingState<List<TrelloAction>> mActionsState;
...
public TObjectListLoadingState getActionsState() {
return mActionsState;
}
...
public List<TrelloAction> getActions() {
return getActionsState().getData();
}
...
public State withActions(List<TrelloAction> actions, LoadState dbLoadState, LoadState serviceLoadState) {
return new State(mBoardState, new TObjectListLoadingState(actions, dbLoadState, serviceLoadState);
}
}
Benefits:
- Extendable. Every piece of data that we load can fit this pattern.
- Explicit. Gives a clear indication of where the data was loaded from and what state it currently is in (e.g. we can wait until it's loaded from the service, we can cancel a service load, etc)
- Cruft free. Moves loading state out of the board activity so we don't end up with 50 loading state objects.
Cons:
- GC churn.
- Verboseness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment