Advertisement
sivakfil

Cache concat+first wrong and correct solutions

Apr 11th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package ass.excercises.cvi8.snippets;
  2.  
  3. import io.reactivex.Observable;
  4.  
  5. public class CommonMistakeExample {
  6.    
  7.     public static void main(String[] args) {
  8.         System.out.println("===== Wrong =====");
  9.         wrong();
  10.        
  11.         System.out.println("===== Correct =====");
  12.         correct();
  13.        
  14.         /*  Outputs:
  15.          *  ===== Wrong =====
  16.             Done work in source1
  17.             Done work in source2
  18.             For each: 'source1'
  19.             ===== Correct =====
  20.             Done work in source1
  21.             For each: 'source1'
  22.          */
  23.     }
  24.  
  25.     private static void wrong() {
  26.         Observable.concat(
  27.                 sourceWrong(1),
  28.                 sourceWrong(2)
  29.         )
  30.         .first("N/A")
  31.         .toObservable()
  32.         .forEach(s -> System.out.printf("For each: '%s'\n", s));       
  33.     }
  34.  
  35.     private static Observable<String> sourceWrong(int num) {
  36.         // This is a wrong approach!
  37.        
  38.         // first be do some work
  39.         doWork();
  40.         System.out.println("Done work in source"+num);
  41.         String resultOfWork = "source"+num;
  42.        
  43.         // then we create observable that already has it's content ready
  44.         return Observable.just(resultOfWork);
  45.     }
  46.  
  47.     private static void correct() {
  48.         Observable.concat(
  49.                 sourceCorrect(1)
  50.                 .doOnNext(s -> System.out.println("Note that this is a correct way to log activity of Observable")),
  51.                
  52.                 sourceCorrect(2)
  53.         )
  54.         .first("N/A")
  55.         .toObservable()
  56.         .forEach(s -> System.out.printf("For each: '%s'\n", s));
  57.     }
  58.    
  59.  
  60.     private static Observable<String> sourceCorrect(int num) {
  61.         // this illustrates very simple way of fixing the problem:
  62.        
  63.         // defer work to later: deferred code will be called only when someone
  64.         // will subscribe on Observable and with concat+first combination,
  65.         // it will occur only when first Observable in concat will return empty Observable;
  66.         return Observable.defer(() -> {
  67.             doWork();
  68.             System.out.println("Done work in source"+num);
  69.             String resultOfWork = "source"+num;
  70.             return Observable.just(resultOfWork);
  71.         });
  72.     }
  73.  
  74.     private static void doWork() {
  75.         try {
  76.             Thread.sleep(500);
  77.         } catch (InterruptedException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement