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.SIZE
は128
ですが、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