RxJava2 / RxAndroid和Android开发的新手,但对Java非常熟悉。但是,在尝试“优化”并能够在对同一资源的一堆调用之间更新UI时,遇到了很多障碍。
我的代码如下:
private int batch = 0;
private int totalBatches = 0;
private List<ItemInfo> apiRetItems = new ArrayList<>();
private Observable<ItemInfo[]> apiGetItems(int[] ids) {
int batchSize = 100;
return Observable.create(emitter -> {
int[] idpart = new int[0];
for(int i = 0; i < ids.length; i += batchSize) {
batch++;
idpart = Arrays.copyOfRange(ids, i, Math.min(ids.length, i+batchSize));
ItemInfo[] items = client.items().get(idpart);
emitter.onNext(items);
}
emitter.onComplete();
}).doOnSubscribe( __ -> {
Log.d("GW2DB", "apiGetItems subscribed to with " + ids.length + " ids.");
totalBatches = (int)Math.ceil(ids.length / batchSize);
progressbarUpdate(0, totalBatches);
}).doOnNext(items -> {
Log.d("GW2DB", batch + " batches of " + totalBatches + " batches completed.");
progressbarUpdate(batch, totalBatches);
}).doOnComplete( () -> {
Log.d("GW2DB", "Fetching items completed!");
progressbarReset();
});
}
[如果删除doOnSubscribe
,doOnNext
和doOnComplete
,则在Android Studio中不会收到任何错误,但是如果我使用其中的任何一个,都会得到Incompatible types. Required: Observable<[...].ItemInfo[]>. Found: Observable<java.lang.Object>
我正在使用RxAndroid 2.1.1和RxJava 2.2.16。
有什么想法吗?
由于添加了方法调用链,因此编译器无法正确猜测Observable.create
中通用参数的类型。您可以使用Observable.<ItemInfo[]>create(...)
进行显式设置。