Jul 30, 2017 · 1 min read
Thanks, i see the pattern. It would help if the documentation is updated to reflect its usage in a ViewModel. I’m assuming the ViewModel loads the LiveData during its construction and any duplicate requests (like the one in response to orientation change) is going to get the cached result from LiveData. Any refresh logic from UI will have the ViewModel create a new instance of the LiveData (via loadUser) and hook into the logic of !isFresh().
public LiveData<Resource<User>> loadUser(final String userId) {
return new NetworkBoundResource<User,User>() { ... @Override
protected boolean shouldFetch(@Nullable User data) {
return rateLimiter.canFetch(userId) && (data == null || !isFresh(data));
}
...
}.getAsLiveData();
}