• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Activity-Service communication: RxSubject as a static Service field

Fen1kz

Lurker
I have UploadingService class:

Java:
public class UploadingService extends Service {
        private CompositeSubscription mSubscription;
        public static PublishSubject<Entity> uploadSubj = PublishSubject.create();
        public static PublishSubject<Boolean> cancelSubj = PublishSubject.create();

        @Override
        public void onCreate() {
            super.onCreate();
            mSubscription = new CompositeSubscription();

            /* Start up code to upload pending entities from database */
            // blah blah, wait for internet, then get all items and apply upload();
            /* end */
          
            mSubscription.add(uploadSubj
              .observeOn(Schedulers.io())
              .filter(o -> RxNetwork.getConnectivityStatus(this))
              .flatMap(this::upload)
              .subscribe());
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
        }

        private Observable<Response> upload(Entity e) {}

        @Override
        public void onDestroy() {
            super.onDestroy();
            mSubscription.unsubscribe();
        }
    }

I start it in the main activity, and i use it in my another activity like this:
Java:
Entity entity = new Entity();
    UploadingService.uploadSubj.onNext(entity);

I also plan to cancel requests same way:
Java:
UploadingService.cancelSubj.onNext(true);

Is that legal/safe to use such static subject? Will it create situations where service is stopped, somehow and app won't upload a thing. If not - how can i give commands to service in rx-way?
 
Back
Top Bottom