本文整理汇总了Java中io.reactivex.FlowableOnSubscribe类的典型用法代码示例。如果您正苦于以下问题:Java FlowableOnSubscribe类的具体用法?Java FlowableOnSubscribe怎么用?Java FlowableOnSubscribe使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FlowableOnSubscribe类属于io.reactivex包,在下文中一共展示了FlowableOnSubscribe类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: insertSystemAlbumAndRefresh
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
/**
* 插入到系统相册, 并刷新系统相册
* @param imageUrl
*/
private static void insertSystemAlbumAndRefresh(final String imageUrl) {
Flowable.create(new FlowableOnSubscribe<Object>() {
@Override
public void subscribe(FlowableEmitter<Object> e) throws Exception {
File file = FileUtils.createFileFrom(imageUrl);
String imageUri = MediaStore.Images.Media.insertImage(ApplicationProvider.IMPL.getApp().getContentResolver(), file.getAbsolutePath(), file.getName(), "图片: " + file.getName());
Log.d("_stone_", "insertSystemAlbumAndRefresh-subscribe: imageUri=" + imageUri);
//讲数据插入到系统图库, 在系统相册APP中就可以看到保存的图片了.
//为了保险起见, 再同步一下系统图库
syncAlbum(imageUrl);
}
}, BackpressureStrategy.BUFFER)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
示例2: processImage
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
private void processImage(@NonNull final Bitmap bitmap) {
//Create observable
Flowable<List<Classifier.Recognition>> observable = Flowable
.create(new FlowableOnSubscribe<List<Classifier.Recognition>>() {
@Override
public void subscribe(FlowableEmitter<List<Classifier.Recognition>> emitter) throws Exception {
emitter.onNext(mTensorFlowImageClassifier.recognizeImage(bitmap));
}
}, BackpressureStrategy.MISSING);
//Create observer
Consumer<List<Classifier.Recognition>> mTfProcessorObserver = new Consumer<List<Classifier.Recognition>>() {
@Override
public void accept(@io.reactivex.annotations.NonNull List<Classifier.Recognition> recognitions) throws Exception {
//Write the result on socket.
String resultStr = "";
for (Classifier.Recognition result : recognitions)
resultStr = resultStr + result.getTitle() + " " + result.getConfidence() + "<br/>";
mSocketWriter.writeMessage(resultStr);
//Remove previous disposables.
mCompositeDisposable.clear();
}
};
//Start the observable
mCompositeDisposable.add(observable.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(mTfProcessorObserver));
}
示例3: asyncMethod
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
private void asyncMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
Flowable.create(new FlowableOnSubscribe<Object>() {
@Override
public void subscribe(FlowableEmitter<Object> e) throws Exception {
Looper.prepare();
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Looper.loop();
}
}
, BackpressureStrategy.BUFFER)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
示例4: doPullStreamPost
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
@Override
public Flowable doPullStreamPost(final IRequestEntity requestEntity, final File file) {
Flowable flowable = DefaultFlowable.create(new FlowableOnSubscribe() {
@Override
public void subscribe(FlowableEmitter emitter) throws Exception {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Gson gson = new Gson();
String json = gson.toJson(requestEntity);
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(mUrl)
.post(body)
.build();
Response response = mClient.newCall(request).execute();
if (response == null) {
emitter.onError(new EmptyException());
return;
}
recvFile(emitter, response, file);
emitter.onComplete();
}
});
return flowable;
}
示例5: createYourOwnTibco
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
@Test
public void createYourOwnTibco() throws Exception {
Flowable<String> flowable = Flowable.create(new FlowableOnSubscribe<String>() {
@Override
public void subscribe(FlowableEmitter<String> e) throws Exception {
while (!e.isCancelled()) {
long numberRecords = e.requested();
System.out.println(numberRecords);
if (numberRecords > 0) {
}
}
}
}, BackpressureStrategy.BUFFER);
flowable.map(x -> x + "Yay!").subscribe(System.out::println);
}
示例6: getTiles
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
/**
* @param path the root directory containing files to be emitted
* @param depth offset, where 1 == data directory, 2 == zoom directory, 3 == row, 4 == column
* @return a stream of file references to data structured as the Google tiling scheme.
*/
public static Observable<File> getTiles(String path, int depth) {
// programmer's note:
// https://medium.com/we-are-yammer/converting-callback-async-calls-to-rxjava-ebc68bde5831
// http://vlkan.com/blog/post/2016/07/20/rxjava-backpressure/
// Essentially lots of way to skin this cat - e.g. onBackpressureBlock /
// reactive pull
return Flowable.create(new FlowableOnSubscribe<File>() {
@Override
public void subscribe(FlowableEmitter<File> subscriber) throws Exception {
try {
// warning: depth 4 is not really walking (semantics)
walk(path, depth, subscriber);
if (!subscriber.isCancelled()) {
subscriber.onComplete();
}
} catch (final Exception ex) {
if (!subscriber.isCancelled()) {
subscriber.onError(ex);
}
}
}
}, BackpressureStrategy.BUFFER).toObservable();
}
示例7: flowable
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
@Override
public Flowable<T> flowable(BackpressureStrategy mode) {
return Flowable.create(new FlowableOnSubscribe<T>() {
@Override
public void subscribe(FlowableEmitter<T> emitter) throws Exception {
final CloseableIterator<T> iterator = mCallable.call();
try {
while (!emitter.isCancelled() && iterator.hasNext()) {
emitter.onNext(iterator.next());
}
emitter.onComplete();
} finally {
iterator.close();
}
}
}, mode);
}
示例8: createFlowable
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
@Override
public final Flowable<RxTaskMessage<T, U>> createFlowable() {
return Flowable.create(new FlowableOnSubscribe<RxTaskMessage<T, U>>() {
@Override
public void subscribe(final FlowableEmitter<RxTaskMessage<T, U>> e) throws Exception {
TaskLogger.v(TAG, "subscribe start");
T innerResult = run(new TaskAgent<U>() {
@Override
public void publishProgress(U progressObject) {
TaskLogger.v(TAG, "onProgress");
e.onNext(RxTaskMessage.<T, U>createProgress(progressObject));
}
});
e.onNext(RxTaskMessage.<T, U>createResult(innerResult));
e.onComplete();
TaskLogger.v(TAG, "subscribe end");
}
}, BackpressureStrategy.ERROR);
}
示例9: queryProvinces
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
/**
* 查询全国所有的省,从数据库查询
*/
private void queryProvinces() {
getToolbar().setTitle("选择省份");
Flowable.create((FlowableOnSubscribe<String>) emitter -> {
if (provincesList.isEmpty()) {
provincesList.addAll(WeatherDB.loadProvinces(DBManager.getInstance().getDatabase()));
}
dataList.clear();
for (Province province : provincesList) {
emitter.onNext(province.mProName);
}
emitter.onComplete();
}, BackpressureStrategy.BUFFER)
.compose(RxUtil.ioF())
.compose(RxUtil.activityLifecycleF(this))
.doOnNext(proName -> dataList.add(proName))
.doOnComplete(() -> {
mProgressBar.setVisibility(View.GONE);
currentLevel = LEVEL_PROVINCE;
mAdapter.notifyDataSetChanged();
})
.subscribe();
}
示例10: queryCities
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
/**
* 查询选中省份的所有城市,从数据库查询
*/
private void queryCities() {
getToolbar().setTitle("选择城市");
dataList.clear();
mAdapter.notifyDataSetChanged();
Flowable.create((FlowableOnSubscribe<String>) emitter -> {
cityList = WeatherDB.loadCities(DBManager.getInstance().getDatabase(), selectedProvince.mProSort);
for (City city : cityList) {
emitter.onNext(city.mCityName);
}
emitter.onComplete();
}, BackpressureStrategy.BUFFER)
.compose(RxUtil.ioF())
.compose(RxUtil.activityLifecycleF(this))
.doOnNext(proName -> dataList.add(proName))
.doOnComplete(() -> {
currentLevel = LEVEL_CITY;
mAdapter.notifyDataSetChanged();
mRecyclerView.smoothScrollToPosition(0);
})
.subscribe();
}
示例11: googleObservable
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
private Flowable<GoogleSignInResult> googleObservable(Activity activity) {
return Flowable.create((FlowableOnSubscribe<ConnectionResult>) e -> {
e.onNext(mGoogleApiClient.blockingConnect(GOOGLE_API_CONNECTION_TIMEOUT_SECONDS,
TimeUnit.SECONDS));
e.onComplete();
},
BackpressureStrategy.DROP)
.subscribeOn(Schedulers.io())
.flatMap(connectionResult -> {
if (!connectionResult.isSuccess()) {
return Flowable.error(new LoginException(LoginException.GOOGLE_ERROR,
connectionResult.getErrorCode(), connectionResult.getErrorMessage()));
}
return Flowable.create(new GoogleSubscriber(this), BackpressureStrategy.DROP);
})
.doOnSubscribe(subscription -> activity.startActivityForResult(getGoogleSingInIntent(),
RC_SIGN_IN))
.doOnTerminate(() -> {
mGoogleApiClient.disconnect();
mGoogleApiClient = null;
mGoogleCallback = null;
});
}
示例12: getRealmItems
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
public static <T extends RealmModel> Flowable<RealmResults<T>> getRealmItems(Class clazz, HashMap<String, String> map) {
return Flowable.create(new FlowableOnSubscribe<RealmResults<T>>() {
@Override
public void subscribe(FlowableEmitter<RealmResults<T>> emitter)
throws Exception {
Realm realm = Realm.getDefaultInstance();
RealmQuery<T> query = realm.where(clazz);
if (map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
query.equalTo(entry.getKey(), entry.getValue());
}
}
RealmResults<T> results = query.findAll();
final RealmChangeListener<RealmResults<T>> listener = _realm -> {
if (!emitter.isCancelled()) {
emitter.onNext(results);
}
};
emitter.setDisposable(Disposables.fromRunnable(() -> {
results.removeChangeListener(listener);
realm.close();
}));
results.addChangeListener(listener);
emitter.onNext(results);
}
}, BackpressureStrategy.LATEST);
}
示例13: createData
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
/**
* 生成Flowable
* @param <T>
* @return
*/
public static <T> Flowable<T> createData(final T t) {
return Flowable.create(new FlowableOnSubscribe<T>() {
@Override
public void subscribe(FlowableEmitter<T> emitter) throws Exception {
try {
emitter.onNext(t);
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
}
}, BackpressureStrategy.BUFFER);
}
示例14: save
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
private Publisher<DownloadStatus> save(final Response<ResponseBody> response) {
return Flowable.create(new FlowableOnSubscribe<DownloadStatus>() {
@Override
public void subscribe(FlowableEmitter<DownloadStatus> e) throws Exception {
record.save(e, response);
}
}, BackpressureStrategy.LATEST);
}
示例15: from
import io.reactivex.FlowableOnSubscribe; //导入依赖的package包/类
@Override
public <E> Flowable<RealmList<E>> from(Realm realm, final RealmList<E> list) {
final RealmConfiguration realmConfig = realm.getConfiguration();
return Flowable.create(new FlowableOnSubscribe<RealmList<E>>() {
@Override
public void subscribe(final FlowableEmitter<RealmList<E>> emitter) throws Exception {
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final Realm observableRealm = Realm.getInstance(realmConfig);
listRefs.get().acquireReference(list);
final RealmChangeListener<RealmList<E>> listener = new RealmChangeListener<RealmList<E>>() {
@Override
public void onChange(RealmList<E> results) {
if (!emitter.isCancelled()) {
emitter.onNext(list);
}
}
};
list.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
list.removeChangeListener(listener);
observableRealm.close();
listRefs.get().releaseReference(list);
}
}));
// Emit current value immediately
emitter.onNext(list);
}
}, BACK_PRESSURE_STRATEGY);
}