finish activity: Long running save/send data to server
Rainer-Lang opened this issue · comments
Do you have any suggestions for such a usecase with rxjava?
- user: backpress/leaves activity
- save/send data to server -> potentially long running task
An Intent service will work well for any one-off task that needs to outlive an activity. If you need a longer running task or more lifecycle control, then a service would be a better choice.
One use case I can think off is file transfers. Lot's of things that can go wrong, and can take a long time to finish.
@marukami Could you point me to some samples?
A fire and forget example would look a bit like the code below.
If you need to be able to cancel the tasks, or handle more complex flows, then I don't know of any examples. In the next week or so I should have time to create some examples. If you need something now, just experiment with using Rx in Services.
@kaushikgopal Have you done anything along these lines?
public class CountIntentService extends IntentService {
public CountIntentService() {
super("CountIntentService");
}
public CountIntentService(String name) {
super(name);
}
@Override protected void onHandleIntent(Intent intent) {
// Some long running task
Observable.timer(10, TimeUnit.SECONDS)
.flatMap(new Func1<Long, Observable<Long>>() {
@Override public Observable<Long> call(final Long aLong) {
// Some 2nd long running task
return Observable.timer(10, TimeUnit.SECONDS);
}
})
.subscribe(new Subscriber<Long>() {
@Override public void onCompleted() {
// Finished
}
@Override public void onError(final Throwable e) {
// Error
}
@Override public void onNext(final Long aLong) {
// Last unit of work finished
}
});
}
}
@marukami Thanks. I think Service will fit for me - hopefully. :)
The "send data to server" means sending a picture to the server. This I have to do later/retry in the app automatically if something goes wrong the first time. So "fire and forget" will do it for me.
I'm very interested in every example you could provide.
@marukami I use now an IntentService and it's working fine. Thanks!