2016-09-09 12 views
0

インターネットからアイテムのリストを取得するには、次のコードがあります。Observableから最初の5つのアイテムを取得してネットワークからデータを取得する

Observable<RealmList<Artist>> popArtists = restInterface.getArtists(); 
    compositeSubscription.add(popArtists.subscribeOn(Schedulers.io()) 
    .observeOn(AndroidSchedulers.mainThread()).subscribe(artistsObserver)); 

問題はリストに80以上の項目があり、最初の5項目しか取得しません。これを達成する最良の方法は何ですか?

+0

をemitingするエンドポイントは、あなたが指定できないObservableに(flatMapIterableを使用することができる理由Iterableを実装している、それはです)制限またはページサイズ? – Blackbelt

+2

Observable上で 'take(5)'? –

+0

@ cricket_007 'take(5)'が働いた。ありがとう。 –

答えて

3

takeは、あなたが探して演算子です。 (ここではドキュメントを参照してください。http://reactivex.io/documentation/operators/take.html)を

flatMapIterableがあなたのRealmList変換リストのすべての項目に

Subscription subscription = restInterface.getArtists() 
             .flatMapIterable(l -> l) 
             .take(5) 
             .subscribeOn(Schedulers.io()) 
             .observeOn(androidSchedulers.mainThread()) 
             .subscribe(artistsObserver); 

compositeSubscription.add(subscription); 
0

私は解決策は、受信した結果から、最初の5つの項目を取ることですので、あなたは、サーバー側を制御することはできません推測:

Observable<RealmList<Artist>> popArtists = restInterface.getArtists(); 
compositeSubscription.add(
popArtists.flatMap(list-> Observable.from(list).limit(5)).subscribeOn(Schedulers.io())      
.observeOn(AndroidSchedulers.mainThread()) 
.subscribe(artistsObserver)); 
関連する問題