本文整理匯總了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);
}