Skip to content

Instantly share code, notes, and snippets.

@Zhuinden
Last active July 29, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zhuinden/ec84d1be0da53a29ec7ffc837ecf639e to your computer and use it in GitHub Desktop.
Save Zhuinden/ec84d1be0da53a29ec7ffc837ecf639e to your computer and use it in GitHub Desktop.
MVVM sample: reactive data access example
public class TasksViewModel
extends BaseObservable
implements Observer<List<Task>> {
@Inject
TasksViewModel(...) {
//...
}
private LiveResults<Task> liveResults;
public void start() {
liveResults = tasksRepository.getTasks();
liveResults.observeForever(this);
}
public void stop() {
liveResults.removeObserver(this);
}
@Override
public void onChanged(@Nullable List<Task> tasks) {
if(tasks == null) {
return; // loading...
}
items.clear();
items.addAll(tasks); // <-- update observable list property
notifyPropertyChanged(BR.empty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment