本文整理了Java中rx.Observable.first()
方法的一些代码示例,展示了Observable.first()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.first()
方法的具体详情如下:
包路径:rx.Observable
类名称:Observable
方法名:first
[英]Returns an Observable that emits only the very first item emitted by the source Observable, or notifies of an NoSuchElementException if the source Observable is empty.
Scheduler: first does not operate by default on a particular Scheduler.
[中]返回一个Observable,该Observable只发出源Observable发出的第一个项,或者如果源Observable为空,则通知NoSuchElementException。
调度器:第一个默认情况下不会在特定的调度器上运行。
代码示例来源:origin: jaydenxiao2016/AndroidFire
} else {
return Observable.concat(fromCache, fromNetwork).first();
代码示例来源:origin: THEONE10211024/RxJavaSamples
@OnClick(R.id.btn_start_pseudo_cache)
public void onDemoPseudoCacheClicked() {
_adapter = new ArrayAdapter<>(getActivity(),
R.layout.item_log,
R.id.item_log,
new ArrayList<String>());
_resultList.setAdapter(_adapter);
_initializeCache();
Observable.concat(_getCachedData(), _getFreshData())
.first()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Contributor>() {
@Override
public void onCompleted() {
Timber.d("done loading all data");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "arr something went wrong");
}
@Override
public void onNext(Contributor contributor) {
_contributionMap.put(contributor.login, contributor.contributions);
_adapter.clear();
_adapter.addAll(getListStringFromMap());
}
});
}
代码示例来源:origin: com.netflix.eureka/eureka2-test-utils
/**
* Return {@link RxItem} that will give first value of the provided observable or will
* time out. The timeout is measured from the time this method is called.
*/
public static <T> RxItem<T> firstFrom(long timeout, TimeUnit timeUnit, final Observable<T> observable) {
return new SingleValueRxItem<T>(TimeUnit.MILLISECONDS.convert(timeout, timeUnit), observable.first());
}
代码示例来源:origin: com.netflix.eureka2/eureka-test-utils
/**
* Return {@link RxItem} that will give first value of the provided observable or will
* time out. The timeout is measured from the time this method is called.
*/
public static <T> RxItem<T> firstFrom(long timeout, TimeUnit timeUnit, final Observable<T> observable) {
return new SingleValueRxItem<T>(TimeUnit.MILLISECONDS.convert(timeout, timeUnit), observable.first());
}
代码示例来源:origin: com.netflix.eureka2/eureka-test-utils
/**
* Return {@link RxItem} that will give list of first values of the provided observables or will
* time out. The timeout is measured from the time this method is called.
*/
public static <T> RxItem<List<T>> firstFromEach(long timeout, TimeUnit timeUnit, final Observable<T>... observables) {
List<Observable<T>> firstItems = new ArrayList<Observable<T>>(observables.length);
for (Observable<T> o : observables) {
firstItems.add(o.first());
}
return new ListRxItem<T>(TimeUnit.MILLISECONDS.convert(timeout, timeUnit), firstItems);
}
代码示例来源:origin: leeowenowen/rxjava-examples
@Override
public void run() {
Observable.range(1, 10).first().subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
log(integer);
}
});
}
});
代码示例来源:origin: com.netflix.rxjava/rxjava-core
/**
* Returns the first item emitted by this {@code BlockingObservable}, or throws
* {@code NoSuchElementException} if it emits no items.
*
* @return the first item emitted by this {@code BlockingObservable}
* @throws NoSuchElementException
* if this {@code BlockingObservable} emits no items
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#first-and-firstordefault">RxJava Wiki: first()</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229177.aspx">MSDN: Observable.First</a>
*/
public T first() {
return blockForSingle(o.first());
}
代码示例来源:origin: com.netflix.eureka/eureka2-test-utils
/**
* Return {@link RxItem} that will give list of first values of the provided observables or will
* time out. The timeout is measured from the time this method is called.
*/
public static <T> RxItem<List<T>> firstFromEach(long timeout, TimeUnit timeUnit, final Observable<T>... observables) {
List<Observable<T>> firstItems = new ArrayList<Observable<T>>(observables.length);
for (Observable<T> o : observables) {
firstItems.add(o.first());
}
return new ListRxItem<T>(TimeUnit.MILLISECONDS.convert(timeout, timeUnit), firstItems);
}
代码示例来源:origin: MaksTuev/ferro
/**
* @return book with bookId
*/
public Observable<Book> getBook(String bookId) {
return Observable.timer(3, TimeUnit.SECONDS)
.flatMap(t -> Observable.from(books))
.filter(book -> book.getId().equals(bookId))
.first();
}
代码示例来源:origin: jacek-marchwicki/JavaWebsocketClient
@Nonnull
public Observable<DataMessage> sendMessageOnceWhenConnected(final Func1<String, Observable<Object>> createMessage) {
return connectedAndRegistered
.compose(isConnected())
.first()
.flatMap(new Func1<RxObjectEventConn, Observable<DataMessage>>() {
@Override
public Observable<DataMessage> call(final RxObjectEventConn rxEventConn) {
return requestData(rxEventConn, createMessage);
}
});
}
代码示例来源:origin: NimbleDroid/FriendlyDemo
@NonNull
private Observable<List<Post>> getPostsFromDB() {
return db.get().createQuery(TABLE_NAME, Post.SELECT_ALL)
.mapToList(Post.MAPPER::map)
.first();
}
代码示例来源:origin: Aptoide/aptoide-client-v8
public Single<Notification> getLastShowed(Integer[] notificationType) {
return getAllSorted(Sort.DESCENDING, notificationType).first()
.map(notifications -> {
for (Notification notification : notifications) {
if (!notification.isDismissed()) {
return notification;
}
}
return null;
})
.toSingle();
}
代码示例来源:origin: hantsy/spring-reactive-sample
Single<Post> findById(Long id) {
return this.db.select("select * from posts where id=?")
.parameter(id)
.get(
rs -> new Post(rs.getLong("id"),
rs.getString("title"),
rs.getString("content")
)
)
.first()
.toSingle();
}
代码示例来源:origin: bravekingzhang/CleanArch
@Override
public Observable<List<SampleModel>> lists(int count, int page) {
Realm realm = Realm.getDefaultInstance();
Observable observable = realm.where(SampleModel.class).findAll().asObservable().map(new Func1<RealmResults<SampleModel>, List<SampleModel>>() {
@Override
public List<SampleModel> call(RealmResults<SampleModel> sampleModels) {
return Converter.RealmResultList2SampleModel(sampleModels);
}
});
return observable.first();
}
//realm maybe have some bugs....,fuck
代码示例来源:origin: ArturVasilov/AndroidSchool
@Test
public void testSimpleObservable() throws Exception {
Observable.just(5, 6, 7)
.reduce((integer, integer2) -> integer + integer2)
.first()
.subscribe(integer -> assertEquals(18, integer.intValue()));
}
代码示例来源:origin: Aptoide/aptoide-client-v8
@Override public Completable removeDownload(String md5) {
return downloadsRepository.getDownload(md5)
.first()
.flatMap(download -> getAppDownloader(download.getMd5()).flatMap(
appDownloader -> appDownloader.removeAppDownload()
.andThen(downloadsRepository.remove(md5))
.andThen(Observable.just(download))))
.doOnNext(download -> removeDownloadFiles(download))
.toCompletable();
}
代码示例来源:origin: Aptoide/aptoide-client-v8
@Override public Completable pauseDownload(String md5) {
return downloadsRepository.getDownload(md5)
.first()
.map(download -> {
download.setOverallDownloadStatus(Download.PAUSED);
downloadsRepository.save(download);
return download;
})
.flatMap(download -> getAppDownloader(download.getMd5()))
.flatMapCompletable(appDownloader -> appDownloader.pauseAppDownload())
.toCompletable();
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_161() throws Exception {
Observable<Person> person = Observable.just(new Person());
Observable<Income> income = person
.flatMap(this::determineIncome)
.flatMap(
Observable::just,
th -> Observable.empty(),
Observable::empty)
.concatWith(person.flatMap(this::guessIncome))
.first();
}
代码示例来源:origin: Aptoide/aptoide-client-v8
private Observable<Download> handleDownloadProgress(AppDownloader appDownloader) {
return appDownloader.observeDownloadProgress()
.flatMap(appDownloadStatus -> downloadsRepository.getDownload(appDownloadStatus.getMd5())
.first()
.flatMap(download -> updateDownload(download, appDownloadStatus)))
.filter(download -> download.getOverallDownloadStatus() == Download.COMPLETED)
.doOnNext(download -> removeAppDownloader(download.getMd5()))
.doOnNext(download -> downloadAnalytics.onDownloadComplete(download.getMd5(),
download.getPackageName(), download.getVersionCode()))
.takeUntil(download -> download.getOverallDownloadStatus() == Download.COMPLETED);
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_570() throws Exception {
Observable<Car> fromCache = loadFromCache();
Observable<Car> fromDb = loadFromDb();
Observable<Car> found = Observable
.concat(fromCache, fromDb)
.first();
}
内容来源于网络,如有侵权,请联系作者删除!