Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. MutableLiveData<List<Integer>> src = new MutableLiveData<>();
  2. List<Integer> value = new ArrayList<>();
  3. for (int i = 0; i < 10; i++) {
  4. value.add(i * 2);
  5. }
  6. src.setValue(value);
  7.  
  8. LiveData<List<String>> dst = Transformations.switchMap(src, input -> {
  9. final MutableLiveData<List<String>> mld = new MutableLiveData<>();
  10. new Thread() {
  11. @Override
  12. public void run() {
  13. Log.d(TAG, "run " + Thread.currentThread());
  14. try {
  15. Thread.sleep(3000);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. List<String> list = new ArrayList<>();
  20. for (Integer i : input) {
  21. list.add("-- " + i + " --");
  22. }
  23. Log.d(TAG, "run " + Thread.currentThread() + " " + list);
  24. mld.postValue(list);
  25. }
  26. }.start();
  27. return mld;
  28. });
  29. dst.observe(this, strings -> {
  30. Log.d(TAG, "onCreate " + Thread.currentThread() + " " + strings);
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement