use of io.reactivex.ObservableSource in project android-mvp-architecture by MindorksOpenSource.
the class AppDataManager method seedDatabaseOptions.
@Override
public Observable<Boolean> seedDatabaseOptions() {
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
final Gson gson = builder.create();
return mDbHelper.isOptionEmpty().concatMap(new Function<Boolean, ObservableSource<? extends Boolean>>() {
@Override
public ObservableSource<? extends Boolean> apply(Boolean isEmpty) throws Exception {
if (isEmpty) {
Type type = new TypeToken<List<Option>>() {
}.getType();
List<Option> optionList = gson.fromJson(CommonUtils.loadJSONFromAsset(mContext, AppConstants.SEED_DATABASE_OPTIONS), type);
return saveOptionList(optionList);
}
return Observable.just(false);
}
});
}
use of io.reactivex.ObservableSource in project RxJavaInAction by fengzhizi715.
the class FlatMapForParallel3 method main.
public static void main(String[] args) {
int threadNum = Runtime.getRuntime().availableProcessors() + 1;
final ExecutorService executor = Executors.newFixedThreadPool(threadNum);
final Scheduler scheduler = Schedulers.from(executor);
Observable.range(1, 100).flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
return Observable.just(integer).subscribeOn(scheduler).map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return integer.toString();
}
});
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
executor.shutdown();
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String str) throws Exception {
System.out.println(str);
}
});
}
use of io.reactivex.ObservableSource in project atlas by alibaba.
the class MergeExcutorServices method main.
public static void main(String[] args) throws InterruptedException {
final MergeObject mergeTask = new MergeObject(null, null, null);
final MergeObject mergeTask1 = new MergeObject(null, null, null);
String[] aa = new String[] { "a", "b", "c", "d", "e" };
final CountDownLatch countDownLatch = new CountDownLatch(1);
Observable.fromArray(aa).flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String s) throws Exception {
return Observable.just(s).map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s + s;
}
}).subscribeOn(Schedulers.computation());
}
}).observeOn(Schedulers.newThread()).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
System.out.println(value);
System.out.println("xxxx");
}
@Override
public void onError(Throwable e) {
System.out.println("onError");
}
@Override
public void onComplete() {
System.out.println("onComplete");
countDownLatch.countDown();
}
});
countDownLatch.await();
// Observable.just(1,2,3,4).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe0 +"+Thread.currentThread().getName());
//
// }
// }).map(new Function<Integer, Integer>() {
//
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map0 +"+Thread.currentThread().getName());
//
// return integer;
// }
// }).subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map +"+Thread.currentThread().getName());
// return integer+1;
// }
// }).subscribeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map5 +"+Thread.currentThread().getName());
//
// return integer;
// }
// }).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe +"+Thread.currentThread().getName());
//
//
// }
// }).subscribeOn(Schedulers.io()).observeOn(Schedulers.single()).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe1 +"+Thread.currentThread().getName());
//
// }
// }).subscribeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map1 +"+Thread.currentThread().getName());
//
// return integer+1;
// }
// }).observeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map3 +"+Thread.currentThread().getName());
//
// return integer;
// }
// }).blockingSubscribe(new Consumer<Integer>() {
// @Override
// public void accept(Integer integer) throws Exception {
// System.out.println("Consumer +"+integer+Thread.currentThread().getName());
//
// }
// });
// Set<String>set = new HashSet<>();
// set.add("a");
// set.add("b");
// set.add("f");
// set.add("c");
// System.out.println(set.toArray()[0]);
// Thread.sleep(5000);
}
use of io.reactivex.ObservableSource in project EnableHands by LeviWGG.
the class NewsModel method getNewsInfo.
@Override
public void getNewsInfo(final INewsContract.INewsPresenter iNewsPresenter, int id) {
Retrofit retrofit = new ServiceFactory().create(NewsService.BASE_URL);
NewsService newsService = retrofit.create(NewsService.class);
Observable<NewsInfo> observable = newsService.getNewsInfo(id);
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<NewsInfo, ObservableSource<List<NewsInfo.T1348647909107Bean>>>() {
@Override
public ObservableSource<List<NewsInfo.T1348647909107Bean>> apply(NewsInfo newsInfo) throws Exception {
return Observable.just(newsInfo.getT1348647909107());
}
}).filter(new Predicate<List<NewsInfo.T1348647909107Bean>>() {
@Override
public boolean test(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
if (t1348647909107Beans.isEmpty()) {
return false;
}
return true;
}
}).subscribe(new Consumer<List<NewsInfo.T1348647909107Bean>>() {
@Override
public void accept(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
// 遍历新闻信息,去掉没有URL的子项
for (int i = 0; i < t1348647909107Beans.size(); i++) {
if (t1348647909107Beans.get(i).getUrl() == null) {
t1348647909107Beans.remove(i);
}
}
iNewsPresenter.setNewsInfo(t1348647909107Beans);
}
});
// return (Observable<T>) observable;
}
use of io.reactivex.ObservableSource in project EnableHands by LeviWGG.
the class PicturePresenterImp method getWeixinNews.
@Override
public void getWeixinNews() {
Retrofit retrofit = new ServiceFactory().create(BASE_URL);
WeixinService weixinService = retrofit.create(WeixinService.class);
Observable<Weixin> observable = weixinService.getWeixinNews(page, 10, "json", KEY);
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<Weixin, ObservableSource<List<Weixin.ResultBean.ListBean>>>() {
@Override
public ObservableSource<List<Weixin.ResultBean.ListBean>> apply(Weixin weixin) throws Exception {
return Observable.just(weixin.getResult().getList());
}
}).filter(new Predicate<List<Weixin.ResultBean.ListBean>>() {
@Override
public boolean test(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
if (listBeans.isEmpty()) {
return false;
}
return true;
}
}).subscribe(new Consumer<List<Weixin.ResultBean.ListBean>>() {
@Override
public void accept(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
if (page == 1) {
mView.setWeixinNews(listBeans);
} else {
mView.loadMore(listBeans);
}
page++;
}
});
}
Aggregations