Last active
July 29, 2017 21:49
-
-
Save Zhuinden/ec84d1be0da53a29ec7ffc837ecf639e to your computer and use it in GitHub Desktop.
MVVM sample: reactive data access example
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 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