當前位置: 首頁>>代碼示例>>Java>>正文


Java SingleOnSubscribe類代碼示例

本文整理匯總了Java中io.reactivex.SingleOnSubscribe的典型用法代碼示例。如果您正苦於以下問題:Java SingleOnSubscribe類的具體用法?Java SingleOnSubscribe怎麽用?Java SingleOnSubscribe使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SingleOnSubscribe類屬於io.reactivex包,在下文中一共展示了SingleOnSubscribe類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: single

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
/**
 * The returned Single emits one Query result as a List.
 */
public static <T> Single<List<T>> single(final Query<T> query) {
    return Single.create(new SingleOnSubscribe<List<T>>() {
        @Override
        public void subscribe(final SingleEmitter<List<T>> emitter) throws Exception {
            final DataSubscription dataSubscription = query.subscribe().single().observer(new DataObserver<List<T>>() {
                @Override
                public void onData(List<T> data) {
                    if (!emitter.isDisposed()) {
                        emitter.onSuccess(data);
                    }
                }
            });
            emitter.setCancellable(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    dataSubscription.cancel();
                }
            });
        }
    });
}
 
開發者ID:greenrobot,項目名稱:ObjectBoxRxJava,代碼行數:25,代碼來源:RxQuery.java

示例2: single

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
private void single() {
    Single.create(new SingleOnSubscribe<String>() {
        @Override
        public void subscribe(SingleEmitter<String> e) throws Exception {
            e.onSuccess("1");
            e.onSuccess("2");
            e.onSuccess("3");
            e.onSuccess("4");
        }
    }).subscribe(new Consumer<String>() {
        @Override
        public void accept(String s) throws Exception {
            Log.e(TAG, "accept: success " + s);
        }
    }, new Consumer<Throwable>() {
        @Override
        public void accept(Throwable throwable) throws Exception {

        }
    });

}
 
開發者ID:penghuanliang,項目名稱:Rxjava2.0Demo,代碼行數:23,代碼來源:CreateActivity.java

示例3: Session

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public Session(Process process, Disposable disposable) {
    this.process = process;
    this.disposable = disposable;
    this.destroy = Completable
            .create(e -> {
                disposable.dispose();
                e.onComplete();
            })
            .subscribeOn(Schedulers.io())
            .doOnComplete(() -> { if (RXSDebug.isDebug()) Timber.tag(TAG).v("destroy():doOnComplete");})
            .doOnError(t -> { if (RXSDebug.isDebug()) Timber.tag(TAG).v(t, "destroy():doOnError");})
            .cache();
    this.waitFor = Single
            .create((SingleOnSubscribe<Integer>) e -> {
                if (RXSDebug.isDebug()) Timber.tag(TAG).d("Waiting for %s to exit.", process);
                int exitCode = process.waitFor();
                if (RXSDebug.isDebug()) Timber.tag(TAG).d("Exitcode: %d, Process: %s", exitCode, process);
                e.onSuccess(exitCode);
            })
            .subscribeOn(Schedulers.io())
            .doOnSuccess(s -> { if (RXSDebug.isDebug()) Timber.tag(TAG).v("waitFor():doOnSuccess %s", s);})
            .doOnError(t -> { if (RXSDebug.isDebug()) Timber.tag(TAG).v(t, "waitFor():doOnError");})
            .cache();
}
 
開發者ID:d4rken,項目名稱:RxShell,代碼行數:25,代碼來源:RxProcess.java

示例4: submit

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public Single<Cmd.Result> submit(Cmd cmd) {
    return Single.create((SingleOnSubscribe<Cmd.Result>) emitter -> {
        QueueCmd item = new QueueCmd(cmd, emitter);
        synchronized (CmdProcessor.this) {
            if (dead) {
                if (RXSDebug.isDebug()) Timber.tag(TAG).w("Processor wasn't running: %s", cmd);
                item.exitCode(Cmd.ExitCode.SHELL_DIED);
                item.emit();
            } else {
                if (RXSDebug.isDebug()) Timber.tag(TAG).d("Submitted: %s", cmd);
                cmdQueue.add(item);
            }
        }
    }).doOnSuccess(item -> {
        if (RXSDebug.isDebug()) {
            Timber.tag(TAG).log(item.getErrors() != null && item.getErrors().size() > 0 ? Log.WARN : Log.INFO, "Processed: %s", item);
        }
    });
}
 
開發者ID:d4rken,項目名稱:RxShell,代碼行數:20,代碼來源:CmdProcessor.java

示例5: saveSnapshotToStorage

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public void saveSnapshotToStorage(final Bitmap bitmap, final String title) {
    final int quality = SPUtils.getBoolean(Constants.SP_HD_SCREENSHOT) ? 100 : 90;
    
    mComposite.add(Single.create(new SingleOnSubscribe<String>() {
        @Override
        public void subscribe(@NonNull SingleEmitter<String> e) throws Exception {
            String address = IOUtils.saveBitmapToExternalStorage(bitmap, title, quality);
            e.onSuccess(address);
        }
    })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                mBaseView.onSnapshotSavedSucceeded(s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mBaseView.onSnapshotSavedFailed(throwable);
            }
        }));
}
 
開發者ID:nichbar,項目名稱:Aequorea,代碼行數:25,代碼來源:ArticlePresenter.java

示例6: getList

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public Single<List<CList>> getList(final int mFilterType, final String orderBy, final String limit, final String skip) {

        return Single.create(new SingleOnSubscribe<List<CList>>() {
            @Override
            public void subscribe(SingleEmitter<List<CList>> emitter) throws Exception {
                Cursor fetchCursor = getCursorByType(mFilterType, orderBy);

                if (fetchCursor == null) {
                    emitter.onError(new Exception("Cursor is null"));
                    return;
                }


                Log.d(TAG, "|Start Filter Type  = " + mFilterType + " " + new Date(System.currentTimeMillis()).toString() + "\n Cursor Count" + fetchCursor.getCount());
                emitter.onSuccess(new ArrayList<>(fillMap(fetchCursor, mFilterType).values()));

            }
        });
    }
 
開發者ID:nitiwari-dev,項目名稱:android-contact-extractor,代碼行數:20,代碼來源:CListExtractorAbstract.java

示例7: deleteCollBookInRx

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
/************************************************************/
public Single<Void> deleteCollBookInRx(CollBookBean bean) {
    return Single.create(new SingleOnSubscribe<Void>() {
        @Override
        public void subscribe(SingleEmitter<Void> e) throws Exception {
            //查看文本中是否存在刪除的數據
            deleteBook(bean.get_id());
            //刪除任務
            deleteDownloadTask(bean.get_id());
            //刪除目錄
            deleteBookChapter(bean.get_id());
            //刪除CollBook
            mCollBookDao.delete(bean);
            e.onSuccess(new Void());
        }
    });
}
 
開發者ID:newbiechen1024,項目名稱:NovelReader,代碼行數:18,代碼來源:BookRepository.java

示例8: toSingle

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public Single<T> toSingle() {
    return Single.create(new SingleOnSubscribe<T>() {
        @Override
        public void subscribe(final @NonNull SingleEmitter<T> e) throws Exception {
            enqueue(new Callback<T>() {
                        @Override
                        public void onResponse(@NonNull T data) {
                            e.onSuccess(data);
                        }
                    }
                    , new ErrorCallback() {
                        @Override
                        public void onError(@NonNull Throwable error) {
                            e.onError(error);
                        }
                    }
            );
        }
    });
}
 
開發者ID:florent37,項目名稱:Android-OkGraphQl,代碼行數:21,代碼來源:AbstractQuery.java

示例9: rename

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
private Single<Integer> rename(RecordingItem recordingItem, int adapterPosition, String name) {
  return Single.create((SingleOnSubscribe<Integer>) e -> {
    File newFile = new File(
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/SoundRecorder/" + name);
    if (newFile.exists() && !newFile.isDirectory()) {
      e.onError(new Exception("File with same name already exists"));
    } else {
      File oldFilePath = new File(recordingItem.getFilePath());
      if (oldFilePath.renameTo(newFile)) {
        recordingItem.setName(name);
        recordingItem.setFilePath(newFile.getPath());
        recordItemDataSource.updateRecordItem(recordingItem);
        e.onSuccess(adapterPosition);
      } else {
        e.onError(new Throwable("Cannot Rename file. Please try again"));
      }
    }
  }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
 
開發者ID:Arjun-sna,項目名稱:Android-AudioRecorder-App,代碼行數:20,代碼來源:PlayListPresenterImpl.java

示例10: single

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public static <T> Single<T> single(final OnSubscribeAction<T> subscribe) {
    final RxActionDelegate<T> delegate = new RxActionDelegate<>();
    return Single.<T>create(new SingleOnSubscribe<T>() {
        @Override
        public void subscribe(@NonNull final SingleEmitter<T> emitter) throws Exception {
            delegate.setDelegate(new ActionDelegate<T>() {
                @Override
                public void onSuccess(T result) {
                    emitter.onSuccess(result);
                }

                @Override
                public void onError(Exception e) {
                    emitter.onError(e);
                }
            });
            subscribe.subscribe(delegate);
        }
    }).doOnDispose(new Action() {
        @Override
        public void run() throws Exception {
            delegate.cancel();
        }
    });
}
 
開發者ID:MobileTribe,項目名稱:pandroid,代碼行數:26,代碼來源:RxActionDelegate.java

示例11: singleWrapped

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
public static <T> Single<Result<T>> singleWrapped(final OnSubscribeAction<T> subscribe) {
    final RxActionDelegate<T> delegate = new RxActionDelegate<>();
    return Single.<Result<T>>create(new SingleOnSubscribe<Result<T>>() {
        @Override
        public void subscribe(@NonNull final SingleEmitter<Result<T>> emitter) throws Exception {
            delegate.setDelegate(new ActionDelegate<T>() {
                @Override
                public void onSuccess(T result) {
                    emitter.onSuccess(new Result<T>(result));
                }

                @Override
                public void onError(Exception e) {
                    emitter.onSuccess(new Result<T>(e));
                }
            });
            subscribe.subscribe(delegate);
        }
    }).doOnDispose(new Action() {
        @Override
        public void run() throws Exception {
            delegate.cancel();
        }
    });
}
 
開發者ID:MobileTribe,項目名稱:pandroid,代碼行數:26,代碼來源:RxActionDelegate.java

示例12: buildSingle

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
/**
 * Given an operation that takes a {@link MaybeConsumer<T>}, create a JavaRX
 * {@link Single<T>} that produces the value passed to the MaybeConsumer.
 *
 * Example:
 * <pre>
 *     // log the name of the experiment with a given id
 *     DataController dc = getDataController();
 *     MaybeConsumers.buildSingle(mc -> dc.getExperimentById(id, mc))
 *                   .subscribe(experiment -> log("Name: " + experiment.getName()));
 * </pre>
 */
public static <T> Single<T> buildSingle(io.reactivex.functions.Consumer<MaybeConsumer<T>> c) {
    return Single.create(new SingleOnSubscribe<T>() {
        @Override
        public void subscribe(SingleEmitter<T> emitter) throws Exception {
            c.accept(new MaybeConsumer<T>() {
                @Override
                public void success(T value) {
                    emitter.onSuccess(value);
                }

                @Override
                public void fail(Exception e) {
                    emitter.onError(e);
                }
            });
        }
    });
}
 
開發者ID:google,項目名稱:science-journal,代碼行數:31,代碼來源:MaybeConsumers.java

示例13: verifyCancellation

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
@Test public void verifyCancellation() {
  final AtomicInteger i = new AtomicInteger();
  //noinspection unchecked because Java
  Single<Integer> source = Single.create(new SingleOnSubscribe<Integer>() {
    @Override public void subscribe(SingleEmitter<Integer> e) {
      e.setCancellable(new Cancellable() {
        @Override public void cancel() {
          i.incrementAndGet();
        }
      });
    }
  });
  MaybeSubject<Integer> lifecycle = MaybeSubject.create();
  source.as(AutoDispose.<Integer>autoDisposable(lifecycle))
      .subscribe();

  assertThat(i.get()).isEqualTo(0);
  assertThat(lifecycle.hasObservers()).isTrue();

  lifecycle.onSuccess(0);

  // Verify cancellation was called
  assertThat(i.get()).isEqualTo(1);
  assertThat(lifecycle.hasObservers()).isFalse();
}
 
開發者ID:uber,項目名稱:AutoDispose,代碼行數:26,代碼來源:AutoDisposeSingleObserverTest.java

示例14: connectService

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
private Single<Ignore> connectService() {
  return Single.create(new SingleOnSubscribe<Ignore>() {
    @Override public void subscribe(final SingleEmitter<Ignore> emitter) throws Exception {
      if (serviceConnection == null) {
        serviceConnection = new ServiceConnection() {
          @Override public void onServiceDisconnected(ComponentName name) {
            rxBillingServiceLogger.log(getTargetClassName(), "Service disconnected");
            appBillingService = null;
            emitter.onError(
                new RxBillingServiceException(RxBillingServiceError.SERVICE_DISCONNECTED));
          }

          @Override public void onServiceConnected(ComponentName name, final IBinder service) {
            rxBillingServiceLogger.log(getTargetClassName(), "Service connected");
            appBillingService = IInAppBillingService.Stub.asInterface(service);
            emitter.onSuccess(Ignore.Get);
          }
        };

        bindService();
      } else {
        emitter.onSuccess(Ignore.Get);
      }
    }
  });
}
 
開發者ID:miguelbcr,項目名稱:RxBillingService,代碼行數:27,代碼來源:RxBillingServiceImpl.java

示例15: single

import io.reactivex.SingleOnSubscribe; //導入依賴的package包/類
/**
 * @param query
 * @return
 */
@NonNull
@CheckReturnValue
public static Single<DataSnapshot> single(@NonNull final Query query) {
    return Single.create(new SingleOnSubscribe<DataSnapshot>() {
        @Override
        public void subscribe(
                @NonNull final SingleEmitter<DataSnapshot> emit) throws Exception {
            final ValueEventListener listener = listener(emit);

            emit.setCancellable(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    query.removeEventListener(listener);
                }
            });

            query.addListenerForSingleValueEvent(listener);
        }
    });
}
 
開發者ID:yongjhih,項目名稱:rxfirebase,代碼行數:25,代碼來源:RxValue.java


注:本文中的io.reactivex.SingleOnSubscribe類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。