Is it a good practice to hold reference in network call interceptor of value that might change using Dagger2?










0















I want to avoid adding to every network call token and locale parameters, but they are not constant and might change. To achieve that I want to use network interceptor.
Locale string is taken from LocaleManager which holding reference to PreferenceManager... And UserManager has field token which is updated quite often from back-end.
Network modue:



@Provides
@Singleton
@QueryInterceptor
public Interceptor queryInterceptor(@UserLocale final String userLocale)
return new Interceptor()
@Override
public Response intercept(@NonNull Chain chain) throws IOException
Request request = chain.request();
final HttpUrl url = request.url().newBuilder()
.addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
.addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
.addQueryParameter(QUERY_PARAM_LOCALE, userLocale)
.build();
request = request.newBuilder().url(url).build();
return chain.proceed(request);

;



Locale:



 @Provides
@UserLocale
public String userLocale(LocaleManager localeManager)
return localeManager.getCurrentLanguageCode();



and something similar with user token.



Is it a good practice to do so?



Sorry, Dagger2 is still quite new for me.










share|improve this question


























    0















    I want to avoid adding to every network call token and locale parameters, but they are not constant and might change. To achieve that I want to use network interceptor.
    Locale string is taken from LocaleManager which holding reference to PreferenceManager... And UserManager has field token which is updated quite often from back-end.
    Network modue:



    @Provides
    @Singleton
    @QueryInterceptor
    public Interceptor queryInterceptor(@UserLocale final String userLocale)
    return new Interceptor()
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException
    Request request = chain.request();
    final HttpUrl url = request.url().newBuilder()
    .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
    .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
    .addQueryParameter(QUERY_PARAM_LOCALE, userLocale)
    .build();
    request = request.newBuilder().url(url).build();
    return chain.proceed(request);

    ;



    Locale:



     @Provides
    @UserLocale
    public String userLocale(LocaleManager localeManager)
    return localeManager.getCurrentLanguageCode();



    and something similar with user token.



    Is it a good practice to do so?



    Sorry, Dagger2 is still quite new for me.










    share|improve this question
























      0












      0








      0








      I want to avoid adding to every network call token and locale parameters, but they are not constant and might change. To achieve that I want to use network interceptor.
      Locale string is taken from LocaleManager which holding reference to PreferenceManager... And UserManager has field token which is updated quite often from back-end.
      Network modue:



      @Provides
      @Singleton
      @QueryInterceptor
      public Interceptor queryInterceptor(@UserLocale final String userLocale)
      return new Interceptor()
      @Override
      public Response intercept(@NonNull Chain chain) throws IOException
      Request request = chain.request();
      final HttpUrl url = request.url().newBuilder()
      .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
      .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
      .addQueryParameter(QUERY_PARAM_LOCALE, userLocale)
      .build();
      request = request.newBuilder().url(url).build();
      return chain.proceed(request);

      ;



      Locale:



       @Provides
      @UserLocale
      public String userLocale(LocaleManager localeManager)
      return localeManager.getCurrentLanguageCode();



      and something similar with user token.



      Is it a good practice to do so?



      Sorry, Dagger2 is still quite new for me.










      share|improve this question














      I want to avoid adding to every network call token and locale parameters, but they are not constant and might change. To achieve that I want to use network interceptor.
      Locale string is taken from LocaleManager which holding reference to PreferenceManager... And UserManager has field token which is updated quite often from back-end.
      Network modue:



      @Provides
      @Singleton
      @QueryInterceptor
      public Interceptor queryInterceptor(@UserLocale final String userLocale)
      return new Interceptor()
      @Override
      public Response intercept(@NonNull Chain chain) throws IOException
      Request request = chain.request();
      final HttpUrl url = request.url().newBuilder()
      .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
      .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
      .addQueryParameter(QUERY_PARAM_LOCALE, userLocale)
      .build();
      request = request.newBuilder().url(url).build();
      return chain.proceed(request);

      ;



      Locale:



       @Provides
      @UserLocale
      public String userLocale(LocaleManager localeManager)
      return localeManager.getCurrentLanguageCode();



      and something similar with user token.



      Is it a good practice to do so?



      Sorry, Dagger2 is still quite new for me.







      android retrofit dagger-2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 11:02









      Antonis RadzAntonis Radz

      266




      266






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Indded you should not store locale in graph since when configuration will change your locale will not be updated. You could have a "ConfigurationComponent" with its own scope but in your case i'll just remove locale from graph :



          @Provides
          @Singleton
          @QueryInterceptor
          public Interceptor queryInterceptor(final LocaleManager localeManager)
          return new Interceptor()
          @Override
          public Response intercept(@NonNull Chain chain) throws IOException
          Request request = chain.request();
          final HttpUrl url = request.url().newBuilder()
          .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
          .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
          .addQueryParameter(QUERY_PARAM_LOCALE, localeManager.getLocal())
          .build();
          request = request.newBuilder().url(url).build();
          return chain.proceed(request);

          ;






          share|improve this answer























          • I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

            – Antonis Radz
            Nov 13 '18 at 12:32










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279577%2fis-it-a-good-practice-to-hold-reference-in-network-call-interceptor-of-value-tha%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Indded you should not store locale in graph since when configuration will change your locale will not be updated. You could have a "ConfigurationComponent" with its own scope but in your case i'll just remove locale from graph :



          @Provides
          @Singleton
          @QueryInterceptor
          public Interceptor queryInterceptor(final LocaleManager localeManager)
          return new Interceptor()
          @Override
          public Response intercept(@NonNull Chain chain) throws IOException
          Request request = chain.request();
          final HttpUrl url = request.url().newBuilder()
          .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
          .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
          .addQueryParameter(QUERY_PARAM_LOCALE, localeManager.getLocal())
          .build();
          request = request.newBuilder().url(url).build();
          return chain.proceed(request);

          ;






          share|improve this answer























          • I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

            – Antonis Radz
            Nov 13 '18 at 12:32















          0














          Indded you should not store locale in graph since when configuration will change your locale will not be updated. You could have a "ConfigurationComponent" with its own scope but in your case i'll just remove locale from graph :



          @Provides
          @Singleton
          @QueryInterceptor
          public Interceptor queryInterceptor(final LocaleManager localeManager)
          return new Interceptor()
          @Override
          public Response intercept(@NonNull Chain chain) throws IOException
          Request request = chain.request();
          final HttpUrl url = request.url().newBuilder()
          .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
          .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
          .addQueryParameter(QUERY_PARAM_LOCALE, localeManager.getLocal())
          .build();
          request = request.newBuilder().url(url).build();
          return chain.proceed(request);

          ;






          share|improve this answer























          • I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

            – Antonis Radz
            Nov 13 '18 at 12:32













          0












          0








          0







          Indded you should not store locale in graph since when configuration will change your locale will not be updated. You could have a "ConfigurationComponent" with its own scope but in your case i'll just remove locale from graph :



          @Provides
          @Singleton
          @QueryInterceptor
          public Interceptor queryInterceptor(final LocaleManager localeManager)
          return new Interceptor()
          @Override
          public Response intercept(@NonNull Chain chain) throws IOException
          Request request = chain.request();
          final HttpUrl url = request.url().newBuilder()
          .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
          .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
          .addQueryParameter(QUERY_PARAM_LOCALE, localeManager.getLocal())
          .build();
          request = request.newBuilder().url(url).build();
          return chain.proceed(request);

          ;






          share|improve this answer













          Indded you should not store locale in graph since when configuration will change your locale will not be updated. You could have a "ConfigurationComponent" with its own scope but in your case i'll just remove locale from graph :



          @Provides
          @Singleton
          @QueryInterceptor
          public Interceptor queryInterceptor(final LocaleManager localeManager)
          return new Interceptor()
          @Override
          public Response intercept(@NonNull Chain chain) throws IOException
          Request request = chain.request();
          final HttpUrl url = request.url().newBuilder()
          .addQueryParameter(QUERY_PARAM_OS, ANDROID_OS)
          .addQueryParameter(QUERY_PARAM_VERSION, BuildConfig.VERSION_NAME)
          .addQueryParameter(QUERY_PARAM_LOCALE, localeManager.getLocal())
          .build();
          request = request.newBuilder().url(url).build();
          return chain.proceed(request);

          ;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 11:48









          Samuel EminetSamuel Eminet

          1,894723




          1,894723












          • I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

            – Antonis Radz
            Nov 13 '18 at 12:32

















          • I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

            – Antonis Radz
            Nov 13 '18 at 12:32
















          I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

          – Antonis Radz
          Nov 13 '18 at 12:32





          I am storing locale string in shared preferences as well, LocaleManager is basically is only a wrapper for getting string from preferences which is in LanguageModule.. in AppComponent, so after locale change interceptor receiving new stored string. What about if @QueryInterceptor singleton will hold reference to User's entity's Token field when used like locale string in example above? My concern and more clear question is - is it possibility to cause memory leaks when singleton instance have references to my LocaleManager and User?

          – Antonis Radz
          Nov 13 '18 at 12:32



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279577%2fis-it-a-good-practice-to-hold-reference-in-network-call-interceptor-of-value-tha%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo