1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Fen1kz, Apr 13, 2016.

  1. Fen1kz

    Fen1kz Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Apr 13, 2016

    Apr 13, 2016
    1
    0
    5
    Russiah
    I have UploadingService class:

    Code (Java):
    1. public class UploadingService extends Service {
    2.         private CompositeSubscription mSubscription;
    3.         public static PublishSubject<Entity> uploadSubj = PublishSubject.create();
    4.         public static PublishSubject<Boolean> cancelSubj = PublishSubject.create();
    5.  
    6.         @Override
    7.         public void onCreate() {
    8.             super.onCreate();
    9.             mSubscription = new CompositeSubscription();
    10.  
    11.             /* Start up code to upload pending entities from database */
    12.             // blah blah, wait for internet, then get all items and apply upload();
    13.             /* end */
    14.          
    15.             mSubscription.add(uploadSubj
    16.               .observeOn(Schedulers.io())
    17.               .filter(o -> RxNetwork.getConnectivityStatus(this))
    18.               .flatMap(this::upload)
    19.               .subscribe());
    20.         }
    21.  
    22.         @Override
    23.         public int onStartCommand(Intent intent, int flags, int startId) {
    24.             return START_STICKY;
    25.         }
    26.  
    27.         private Observable<Response> upload(Entity e) {}
    28.  
    29.         @Override
    30.         public void onDestroy() {
    31.             super.onDestroy();
    32.             mSubscription.unsubscribe();
    33.         }
    34.     }
    I start it in the main activity, and i use it in my another activity like this:
    Code (Java):
    1. Entity entity = new Entity();
    2.     UploadingService.uploadSubj.onNext(entity);
    I also plan to cancel requests same way:
    Code (Java):
    1. 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?
     

    Advertisement

Share This Page

Loading...