android - RxJava - Consuming a GroupedObservable -
given following code:
bcxclient .fetchtodos() .flatmap(new func1<list<bcxtodo>, observable<bcxtodo>>() { @override public observable<bcxtodo> call(list<bcxtodo> bcxtodos) { return observable.from(bcxtodos); } }) .groupby(new func1<bcxtodo, bcxtodolist>() { @override public bcxtodolist call(bcxtodo bcxtodo) { return bcxtodo.todolist; } }) .tolist();
which returns list<groupedobservable<>>
. in code, need count how many objects there in each groupedobservable. if use count()
function returns observable<int>
. following code won't work:
@override public int getchildrencount(int groupposition) { int count; mtodolist.get(groupposition) .count() .subscribe( new action1<integer>() { @override public void call(integer integer) { count = integer; } }); return count; }
if make local variable final, can't assign value it.
so far can tell, have 2 options. store count each grouping in field belongs class, or transform groupedobservable alternate structure in chain (above). if case, what's best way this?
this code wrote translate groupedobservable:
.flatmap( new func1<groupedobservable<bcxtodolist, bcxtodo>, observable<bcxtodolist>>() { @override public observable<bcxtodolist> call(final groupedobservable<bcxtodolist, bcxtodo> bcxtodolistbcxtodogroupedobservable) { return bcxtodolistbcxtodogroupedobservable .tolist() .flatmap( new func1<list<bcxtodo>, observable<bcxtodolist>>() { @override public observable<bcxtodolist> call(list<bcxtodo> bcxtodos) { bcxtodolistbcxtodogroupedobservable.getkey().todos.addall( bcxtodos ); return observable.just( bcxtodolistbcxtodogroupedobservable.getkey() ); } }); } })
i'm not sure if best way it
your first attemp doesn't work, count primitive value , not object.
to count
, can block , value. (but current thread block)
@override public int getchildrencount(int groupposition) { int count = mtodolist.get(groupposition).toblocking().single(); return count; }
if don't want block, using pair
alike structure seems idea
bcxclient .fetchtodos() .flatmap(observable::from) .groupby(bcxtodo-> bcxtodo.todolist) .flatmap(todo -> todo.count().map(c -> new pair(todo, c)) .tolist() .subscribe(pair -> //** iter on list **//);
Comments
Post a Comment