LoginSignup
3

More than 5 years have passed since last update.

[Android]CombineLatestで15個以上の処理を合成したい

Last updated at Posted at 2015-09-14

Androidアプリ開発で、Rxjavaを使うときの話です。
タイトルの通りですが、ConbineLatistで15個以上の処理を合成しようとすると、例外が発生します。

More than RxRingBuffer.SIZE sources to combineLatest is not supported.

これは、OnSubscribeCombineLatestのコンストラクタで、sources数に制限をかけられているのが原因です。

        if (sources.size() > RxRingBuffer.SIZE) {
            // For design simplicity this is limited to RxRingBuffer.SIZE. If more are really needed we'll need to
            // adjust the design of how RxRingBuffer is used in the implementation below.
            throw new IllegalArgumentException("More than RxRingBuffer.SIZE sources to combineLatest is not supported.");
        }

RxRingBuffer.SIZEという定数は、RxRingBuffer.javaで以下のように初期化されます。
通常RxRingBuffer.SIZE128ですが、Androidの環境下では16と低いデフォルト値が設定されています。

    static int _size = 128;
    static {
        // lower default for Android (https://github.com/ReactiveX/RxJava/issues/1820)
        if (PlatformDependent.isAndroid()) {
            _size = 16;
        }

        // possible system property for overriding
        String sizeFromProperty = System.getProperty("rx.ring-buffer.size"); // also see IndexedRingBuffer
        if (sizeFromProperty != null) {
            try {
                _size = Integer.parseInt(sizeFromProperty);
            } catch (Exception e) {
                System.err.println("Failed to set 'rx.buffer.size' with value " + sizeFromProperty + " => " + e.getMessage());
            }
        }
    }
    public static final int SIZE = _size;

どうしてもRxRingBuffer.SIZEを変更したい場合は、RxRingBuffer.javaが初期化される前に、System.setProperty経由で値を上書きします。以下がサンプルコードです。

public class CustomApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        System.setProperty("rx.ring-buffer.size", "32");
    }
}

関連Issue: ReactiveX/RxJava/issues/1820

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3