Recently I started android project with hard usage of Reactive extensions. I've read some introductions and tutorials, but I'm still at beginner's level. According to this article:
everything is a stream
however my current understanding (or mental barrier) tells me that any operation which mutates state (removing data from repository for example) should not be/return a stream/observable.
Little background about my domain: I have a use case for registering geofences. Since geofences do not survive reboot, I keep track of active geofences in repository. Sometimes app needs to remove geofence, so basic steps to achieve this are:
- retrieve geofence from repository
- remove geofence from device
- remove geofence from repository
my current solution is following:
geofenceRepository.get(id)
.map(new Func1<Geofence, String>() {
@Override
public String call(Geofence geofence) {
geofenceRepository.delete(geofence.getId()); // synchronous call here
return geofence.getRequestId();
}
})
.toList()
.flatMap(new Func1<List<String>, Observable<Status>>() {
@Override
public Observable<Status> call(List<String> ids) {
return locationProvider.removeGeofences(ids);
}
});
where Geofence is my custom data structure and locationProvider is from this nice library.
You'll notice that data retrieval is implemented as stream/observable unlike delete.
What I don't like in above example is: map operator with side effect
Questions
- What would be better solution to be more "reactive", what I'm missing here?
- Does it make sense to use reactive approach at all?
by reactive programming I mean:
programming with asynchronous data streams