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?
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With