1

i'm working on an app in which i'm getting data from rest api , so basically i implemented WorkManager for that and i want the data to refresh every 15 minutes , so i set the minimum value for interval as 15 minutes , i set up my workmanager class in which i make a request to my api , and passed that class to PeriodicTimeRequest , now i dont know how i'm going to set the data to my recyclerview after initiating the request

this is my WorkManager Class

public class WorkManager extends Worker {

   public static List<Movie> list;

    public WorkManager(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        getData();
        return Result.success();
    }


    public void getData() {
        RetrofitBuilder.getData().getTopRatedMovies(Utils.apikey, Utils.language, 1)
                .enqueue(new Callback<Movie>() {
                    @Override
                    public void onResponse(Call<Movie> call, Response<Movie> response) {
                        if (response.isSuccessful()) {
                           list = response.body().getMovies();
                        }
                    }

                    @Override
                    public void onFailure(Call<Movie> call, Throwable t) {
                        Log.d("TODO", "Excep" + t.toString());
                    }
                });

    } 

this is my PeriodicTimeRequest in my activity

  Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(WorkManager.class,
                15, TimeUnit.MINUTES)
                .setConstraints(constraints)
                .build();
         androidx.work.WorkManager.getInstance(MainActivity.this).enqueueUniquePeriodicWork("tags",
                 ExistingPeriodicWorkPolicy.KEEP,periodicWorkRequest);

so i basically want to update data from api every 15 minutes , and then i want to get data and push it to my recyclerview , am i using the right approach for that , thank you guys .

Taki
  • 3,290
  • 1
  • 16
  • 41

1 Answers1

1

No you're not. Static fields are really bad.

Today Realm (and Room I think) has some mechanisms to update recyclerViews automatically when the underlying result of a query change. So that would be the easiest approach.

Another way would be to use LocalBroadcastManager as I recently describe here

Edit:

After further research it happen that workManager provide an output mechanism that is shown here and your question was already answered here.

aiqency
  • 1,015
  • 1
  • 8
  • 22
  • in the case of realm or room , do you mean that i should store the data and then later update recyclerview ? – Taki Mar 24 '20 at 23:02
  • Isn't WorkManager intended to also syncing data with server ? im confused – Taki Mar 24 '20 at 23:15
  • In case of Realm you simply persist the data and the recyclerView reflect the changes without you doing anything https://medium.com/@Zhuinden/why-realm-is-a-great-persistence-solution-for-beginners-in-android-development-6d69698efaad. – aiqency Mar 24 '20 at 23:18
  • ```Isn't WorkManager intended to also syncing data with server``` Yes, the way you use WorkManager is ok, but keeping a list statically instead of using a persistence layer is very bad and make no sense. https://stackoverflow.com/a/19774331/2754562. If you don't want to persist your data then just use ```LocalBroadcastManager``` – aiqency Mar 24 '20 at 23:22
  • so i think the same approach that goes with service for example , with services or workmanager whenever we want to update data , we should provide a persistence layer , but in my case i dont want to persist data , so i ll just pass that list of data in a LocalBroadcastManager , is that correct > – Taki Mar 24 '20 at 23:32
  • Well it depends on the type of service you are using. For example ```bound services``` provide a way to communicate via IPC while ```IntentService``` via intent. But in your case ```LocalBroadcastManager``` woulb be the better choice AFAIK – aiqency Mar 25 '20 at 00:28
  • Thank you so much mate , one last step , i already set up the code like the one you sent me , can you please take a look at it and tell me if this is correct https://pastebin.com/mJbRLvrU – Taki Mar 25 '20 at 01:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210267/discussion-between-aiqency-and-taki-eddine). – aiqency Mar 25 '20 at 01:14