Java Code Examples for io.reactivex.Observable.defer()

The following are Jave code examples for showing how to use defer() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Quran   File: TranslationManagerPresenter.java   View Source Code Vote up 6 votes
Observable<TranslationList> getCachedTranslationListObservable(final boolean forceDownload) {
  return Observable.defer(new Callable<ObservableSource<? extends TranslationList>>() {
    @Override
    public ObservableSource<TranslationList> call() throws Exception {
      boolean isCacheStale = System.currentTimeMillis() -
          quranSettings.getLastUpdatedTranslationDate() > Constants.MIN_TRANSLATION_REFRESH_TIME;
      if (forceDownload || isCacheStale) {
        return Observable.empty();
      }

      try {
        File cachedFile = getCachedFile();
        if (cachedFile.exists()) {
          Moshi moshi = new Moshi.Builder().build();
          JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
          return Observable.just(jsonAdapter.fromJson(Okio.buffer(Okio.source(cachedFile))));
        }
      } catch (Exception e) {
        Crashlytics.logException(e);
      }
      return Observable.empty();
    }
  });
}
 
Example 2
Project: vt-support   File: StorageImpl.java   View Source Code Vote up 6 votes
@Override
public Observable<Metadata> getMetadata() {
  return Observable.defer(() -> {
    final File metadata = new File(directory, "config.json");
    try {
      if (metadata.exists()) {
        final String raw = FileUtils.readFileToString(metadata, "UTF-8");
        final Metadata result = new Metadata.Builder().setTileJson(raw).build();
        return Observable.just(result);
      }
    } catch (final IOException ex) {
      throw Exceptions.propagate(ex);
    }
    return Observable.empty();
  });
}
 
Example 3
Project: Learning-RxJava   File: Ch2_25.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
        Observable<Integer> source = Observable.defer(() ->
                Observable.range(start, count));
        source.subscribe(i -> System.out.println("Observer 1: " + i));
//modify count
        count = 10;
        source.subscribe(i -> System.out.println("Observer 2: " + i));
    }
 
Example 4
Project: GitHub   File: Car.java   View Source Code Vote up 5 votes
public Observable<String> brandDeferObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
        @Override
        public ObservableSource<? extends String> call() throws Exception {
            return Observable.just(brand);
        }
    });
}
 
Example 5
Project: GitHub   File: DisposableExampleActivity.java   View Source Code Vote up 5 votes
static Observable<String> sampleObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
        @Override
        public ObservableSource<? extends String> call() throws Exception {
            // Do some long running operation
            SystemClock.sleep(2000);
            return Observable.just("one", "two", "three", "four", "five");
        }
    });
}
 
Example 6
Project: GitHub   File: MainActivity.java   View Source Code Vote up 5 votes
static Observable<String> sampleObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
      @Override public ObservableSource<? extends String> call() throws Exception {
            // Do some long running operation
            SystemClock.sleep(5000);
            return Observable.just("one", "two", "three", "four", "five");
        }
    });
}
 
Example 7
Project: JD-Test   File: ImageCompressor.java   View Source Code Vote up 5 votes
/**
 * 使用defer操作符 待订阅后再开始压缩任务
 * @param file
 * @return
 */
public Observable<File> compressToFileAsObservable(final File file) {
    return Observable.defer(new Callable<ObservableSource<? extends File>>() {
        @Override
        public Observable<File> call() {
            return Observable.just(compressToFile(file));
        }
    });
}
 
Example 8
Project: JD-Test   File: ImageCompressor.java   View Source Code Vote up 5 votes
public Observable<Bitmap> compressToBitmapAsObservable(final File file) {
    return Observable.defer(new Callable<ObservableSource<? extends Bitmap>>() {
        @Override
        public Observable<Bitmap> call() {
            return Observable.just(compressToBitmap(file));
        }
    });
}
 
Example 9
Project: DeepImagePreview-Project   File: SearchResultHandler.java   View Source Code Vote up 5 votes
private Observable<SearchResultContainer> fakeImageSearch() {
    return Observable.defer(new Callable<ObservableSource<? extends SearchResultContainer>>() {
        @Override
        public ObservableSource<? extends SearchResultContainer> call() throws Exception {
            ArrayList<SearchItem> items = new ArrayList<>();

            items.add(new SearchItem(new ItemImage("http://media.cntraveler.com/photos/542333488614701924e124a9/master/w_775," +
                    "c_limit/italy-food-intro-1.jpg")));
            items.add(new SearchItem(new ItemImage("http://i.ndtvimg.com/i/2016-03/bruschetta-625_625x350_41459344513.jpg")));
            items.add(new SearchItem(new ItemImage("https://s-media-cache-ak0.pinimg" +
                    ".com/originals/bd/1f/89/bd1f899131e589e3e8ca30784e6320d4.jpg")));
            items.add(new SearchItem(new ItemImage("http://az616578.vo.msecnd.net/files/2016/10/01/636109138160187836348256689_food2.jpg")));
            items.add(new SearchItem(new ItemImage("http://media.cntraveler.com/photos/542333488614701924e124a8/master/w_775," +
                    "c_limit/italy-food-bigoli-2.jpg")));
            items.add(new SearchItem(new ItemImage("http://foodsofitalybedford.co.uk/communities/6/000/001/507/226//images/5770578.jpg")));
            items.add(new SearchItem(new ItemImage("http://www.mytorontoscoop.com/rsz_lasagna%20(2).jpg")));
            items.add(new SearchItem(new ItemImage("http://www.italian-feelings.com/wp-content/uploads/2016/02/nord-sud-940x625.jpg")));
            items.add(new SearchItem(new ItemImage("http://www.ndtv.com/cooks/images/pasta-carbonara-ritu-dalmia_article.jpg")));
            items.add(new SearchItem(new ItemImage("http://www.morningadvertiser.co" +
                    ".uk/var/plain_site/storage/images/publications/hospitality/morningadvertiser.co" +
                    ".uk/pub-food/good-food-guide-editor-calls-for-more-veggie-options-on-pub-menus/8422626-1-eng-GB/Good-Food-Guide-editor" +
                    "-calls-for-more-veggie-options-on-pub-menus.jpg")));
            items.add(new SearchItem(new ItemImage("https://static.independent.co" +
                    ".uk/s3fs-public/styles/article_small/public/thumbnails/image/2016/12/19/18/sush0istock-gkrphoto.jpg")));
            items.add(new SearchItem(new ItemImage("http://www.2sfg" +
                    ".com/globalassets/corporate/home-page/2sisters-food-group-roast-banner5.jpg")));
            items.add(new SearchItem(new ItemImage("http://www.oghmapartners.com/wp-content/uploads/2017/05/1417931965.jpg")));

            return Observable.just(new SearchResultContainer(items)).delay(3, TimeUnit.SECONDS);
        }
    });
}
 
Example 10
Project: RxJava4AndroidDemos   File: Defer.java   View Source Code Vote up 5 votes
@Override
public void test1() {
    Log.i(TAG, "test1() Defer simple demo, call() twice");
    Observable<String> observable = Observable.defer(new Callable<ObservableSource<String>>() {
        @Override
        public ObservableSource<String> call() throws Exception {
            Log.d(TAG, "call()");
            return new ObservableSource<String>() {
                @Override
                public void subscribe(@NonNull Observer<? super String> observer) {
                    Log.d(TAG, "subscribe()");
                    for (int i = 0; i < 3; i++) {
                        observer.onNext(String.valueOf(i));
                    }
                }
            };
        }
    });

    for (int time = 0; time < 2; time++) {
        observable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, "accept() s: " + s);
            }
        });
    }
}
 
Example 11
Project: RxJava2-Android-Sample   File: Car.java   View Source Code Vote up 5 votes
public Observable<String> brandDeferObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
        @Override
        public ObservableSource<? extends String> call() throws Exception {
            return Observable.just(brand);
        }
    });
}
 
Example 12
Project: RxJava2-Android-Sample   File: DisposableExampleActivity.java   View Source Code Vote up 5 votes
static Observable<String> sampleObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
        @Override
        public ObservableSource<? extends String> call() throws Exception {
            // Do some long running operation
            SystemClock.sleep(2000);
            return Observable.just("one", "two", "three", "four", "five");
        }
    });
}
 
Example 13
Project: RxAndroidExample   File: MainActivity.java   View Source Code Vote up 5 votes
static Observable<String> sampleObservable() {
    return Observable.defer(new Callable<ObservableSource<? extends String>>() {
        @Override
        public ObservableSource<? extends String> call() throws Exception {
            SystemClock.sleep(5000);
            return Observable.just("one", "two", "three", "four", "five");
        }
    });
}
 
Example 14
Project: vt-support   File: StorageImpl.java   View Source Code Vote up 5 votes
@Override
public Observable<Integer> getMaxZoomLevel() {
  return Observable.defer(() -> {
    final int[] zoomLevels = getZoomLevels();
    return zoomLevels.length > 0 ? Observable.just(zoomLevels[zoomLevels.length - 1])
        : Observable.empty();
  });
}
 
Example 15
Project: vt-support   File: StorageImpl.java   View Source Code Vote up 5 votes
@Override
public Observable<Integer> getMinZoomLevel() {
  return Observable.defer(() -> {
    final int[] zoomLevels = getZoomLevels();
    return zoomLevels.length > 0 ? Observable.just(zoomLevels[0]) : Observable.empty();
  });
}
 
Example 16
Project: ocraft-s2client   File: S2ClientVerticle.java   View Source Code Vote up 4 votes
private Observable<WebSocket> connection() {
    return Observable.defer(() -> httpClient
            .websocketStream(config().getInteger(CFG_PORT), config().getString(CFG_IP), SC2API_URI)
            .toObservable());
}