How is it right to reconstruct the encashed Observable used with Retrofit?



  • Dano:

    API, returning list with data in format JSON

    Objective:

    Receipt of data by force Retrofit+RxJava

    Problem:

    One request should be made initially and not duplicated if the screen was rolled before the end of the task. We also need to be able to reset the task.

    What happened was:

    First, I decided to make Singlton and make the only copy. Observable by cache()

    Second, complete rebuilding of the facility Retrofit (1), copy of retrofit-interface (2) and Observable(3). If 1 and 2 do not do - 3 - remains the same and returns the encrypted data instead of new ones.

    Question:

    The way I reset the task of obtaining data is not good. How better/right to re-create Observable?


    Singleton for receipt/reconstruction Observalbe:

    public class SingltonRetrofit
    {
        private static RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
    
    private static Gson gson = new GsonBuilder().create();
    
    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(rxAdapter)
            .build();
    
    private static GetModels apiService = retrofit.create(GetModels.class);
    private static Observable<ArrayList<Model>> observableModelsList;
    
    public static void reset()
    {
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(rxAdapter)
                .build();
        apiService = retrofit.create(GetModels.class);
        observableModelsList = null;
    }
    
    public static Observable<ArrayList<Model>> getModelsObservable()
    {
        if (observableModelsList == null)
        {
            observableModelsList = apiService.getModelsList().cache();
        }
        return observableModelsList;
    }
    

    }

    P. S.

    The same question is in English: https://stackoverflow.com/questions/38173653/how-to-recreate-or-reset-cached-observable-used-with-retrofit-to-get-new-data



  • I don't want to do better, but... Personally, I keep the data separate from any activism in order to keep the code out of my mouth.

    There's a class that always remembrances when the application starts. For example, the Model

    public class Model {
        public final MyData mayData;
    

    public Model(RestApi mRestApi) { //мне удобно передовать сразу интерфейс апи
    mayData = new MyData(mRestApi);

    }
    }

    It starts in a class of inherited application.

    MyData Class is the normal class in which requests for a specific screen or data type are described.

        public class MyData {
    public MainPageInfo(RestApi retrofit) {
    super(retrofit);
    }

    public void getData() {
        mRestApi.getData().enqueue(new Callback<ArrayList<Data>>() {
            @Override
            public void onResponse(Call<ArrayList<Data>> call, Response<ArrayList<Data>> response) {
    
            }
    
            @Override
            public void onFailure(Call<ArrayList<Data>> call, Throwable t) {
                setError(t);
            }
    
        });
    }
    

    }

    For easy access to the model, I keep references to it in the classes inherited from the fragment or activate them, in turn, I put them in the heirs for the wine I need. As a result, during the coup d ' état, the pen itself will be deconstructed, but not the data, and it is easy to ask the MyData class with a simple null check (to make a request if no data are available). Of course, implementation isn't perfect, as I did here, not the whole code (except there's a listening model). But I don't think it's a problem for you, and you'll decide whether or not this method fits you. The only disadvantage is if all the app is decomposed, so is the data. However, data from any location = may be contacted If you write to the mail, I'll give you some details.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2