3

I want to run a method periodically such that it returns an ArrayList of a custom object. Here is my code snippet,

    subscribe = Observable.interval(5, TimeUnit.SECONDS)
            .map(new Func1<Long, ArrayList<Item>>() {

                @Override
                public ArrayList<Item> call(Long aLong) {
                    return new ArrayList<Item>(aLong.intValue());
                }
            });

However, this gives an error

map(rx.functions.Func1<? super T, ? extends R>)in Observable cannot be applied to (anonymous rx.functions.Func1<java.lang.Long, java.util.ArrayList<com.example.Item>>)

This works fine when the returned value is an ArrayList<String>. I do not understand what the problem is here. Are custom objects not allowed?

yadav_vi
  • 1,289
  • 4
  • 16
  • 46

1 Answers1

1

You don't get subscription on map, you get it after subscribing. Here's sample code for demonstrating it.

  Observable<ArrayList<Item>> observable = Observable.interval(5, TimeUnit.SECONDS)
            .map(new Func1<Long, ArrayList<Item>>() {

                @Override
                public ArrayList<Item> call(Long aLong) {
                    return new ArrayList<Item>(aLong.intValue());
                }
            });
    Subscription subscription = observable.subscribe(new Action1<ArrayList<Item>>() {
        @Override
        public void call(ArrayList<Item> items) {
            //Do something with list items here
        }
    });
jimmy0251
  • 16,293
  • 10
  • 36
  • 39