java - Automatically get items from observable when an event is fired without an eventbus -
what best way automatically retrieve new items observable when have external event tells you should?
for example , lets have , itemrepository
implements getallitems()
method (which returns observable<list<item>>
webservice) , , then, external event (like push notification) tells application data needs refreshed. (also itemrepository
used in presenter
, presenter has called getallitems
, on it's onnext
, it's data s refreshed.)
i know can done event bus (listen event , when fired, fetch again) , i'm wondering if it's possible automatically.
thanks.
edit
this solution came , of sqlbrite library , not sure if best or cleanest way it. have publishsubject events sent to:
publishsubject<object> updateevent;
and in getallitems() method , check events subject:
public observable<list<item>> getall() { observable.onsubscribe<list<item>> subscribe = subscriber -> { updateevent.subscribe(s -> { subscriber.onnext(dbitemrepository.getall()); }); subscriber.onnext(dbitemrepository.getall());// first call }; final observable<list<item>> automatonobservable = observable.create(subscribe) = .onbackpressurelatest() .observeon(schedulers.computation()) .onbackpressurelatest(); return automatonobservable; }
i think rodrigo henriques has right idea, need change bit according question:
public static observable<string> observerepositoryupdates(itemsrepository repo, observable<void> updatetrigger) { updatetrigger //you can insert observeon here .flatmap(event -> repository.getallitems()); } public static void dowork() { itemsrepository repo = new itemsrepository(); publishsubject<void> updatetrigger = publishsubject.create(); observerepositoryupdates(repo, updatetrigger) .subscribe(items -> system.out.println(items.tostring()); updatetrigger.onnext(null);//trigger update }
depending on event, doesn't have publishsubject
, other observable can trigger repository update.
if event happens , want drop running repository update , start new one, can use operator switchmap.
Comments
Post a Comment