首页 > 解决方案 > 无法执行 Single.create 中的任何内容 - rxjava

问题描述

嗨,我有一个store具有 Single.create 的函数,目前只有一条 Timber 日志消息。

Single.create 中的代码不会被执行。

public Single<String> store(final String filePath, final byte[] fileContents) {

    Timber.d("inside store method");

        Single<String> single = Single.create(subscriber -> {
            Timber.d("Inside single");
        });

        return single.subscribeOn(Schedulers.io());
    }
}

这个 Store 函数从两个地方调用,一个来自它所在的类,它工作正常,但是当我从 Single.create 内的不同类代码调用它时,它不会被执行。

从 kotlin 类调用不起作用的调用

关于为什么会发生这种情况的任何建议?

您的帮助建议将不胜感激。

谢谢R

编辑

Single.create 这是 rx 包

   /**
 * Returns a Single that will execute the specified function when a {@link SingleSubscriber} executes it or
 * a {@link Subscriber} subscribes to it.
 * <p>
 * <img width="640" height="200" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.create.png" alt="">
 * <p>
 * Write the function you pass to {@code create} so that it behaves as a Single: It should invoke the
 * SingleSubscriber {@link SingleSubscriber#onSuccess onSuccess} and/or
 * {@link SingleSubscriber#onError onError} methods appropriately.
 * <p>
 * A well-formed Single must invoke either the SingleSubscriber's {@code onSuccess} method exactly once or
 * its {@code onError} method exactly once.
 * <p>
 * <dl>
 * <dt><b>Scheduler:</b></dt>
 * <dd>{@code create} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param <T>
 *            the type of the item that this Single emits
 * @param f
 *            a function that accepts an {@code SingleSubscriber<T>}, and invokes its {@code onSuccess} or
 *            {@code onError} methods as appropriate
 * @return a Single that, when a {@link Subscriber} subscribes to it, will execute the specified function
 * @see <a href="http://reactivex.io/documentation/operators/create.html">ReactiveX operators documentation: Create</a>
 */
public static <T> Single<T> create(OnSubscribe<T> f) {
    return new Single<T>(f);
}

这就是我从 kotlin 调用的方式

 val result = pdfTicketStorage.store(offlinePrintedTicketInfo.pdfTicketFilePath, offlinePrintedTicketInfo.pdfTicketContent)
        Timber.d("OfflineDataLib - postPrintedTicket - result - " + result )

标签: javaandroidkotlinrx-java

解决方案


推荐阅读