When we don't need the whole sequence but only a few elements at the beginning or end, we can use take()
or takeLast()
.
What if we only want the first three elements of an Observable sequence, emit them, and then let the Observable complete? The take()
function gets an N integer as a parameter, emits only the N first elements from the original sequence, and then it completes:
private void loadList(List<AppInfo> apps) { mRecyclerView.setVisibility(View.VISIBLE); Observable.from(apps) .take(3) .subscribe(new Observer<AppInfo>() { @Override public void onCompleted() { mSwipeRefreshLayout.setRefreshing(false); } @Override public void onError(Throwable e) { Toast.makeText(getActivity(), "Something went south!", Toast.LENGTH_SHORT).show(); mSwipeRefreshLayout.setRefreshing(false); ...