0

I'm using Retrofit2 and DBFlow. I have a problem when I save my data into my database. Certain data are duplicated when I'm calling the thread twice at the same time. The problem is my List because this variable is final. And I have to set final because I need to use this List in my thread.

Then, there is a way to remove the final to my List and replace by something ?

Retrofit onResponse()

public void onResponse(Call<AdminPictures> call, Response<AdminPictures> response) {
        AdminPictures apResponse = response.body();

        // Here is my list
        final List<PictureInfos> pictureInfos = apResponse.getPicturesList();

        new Thread(new Runnable() {
            @Override
                public void run() {
                    try {
                        // The List is used here
                        for (PictureInfos infos : pictureInfos) {
                           if(!infos.exists()){
                              infos.save();
                           }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
John
  • 4,711
  • 9
  • 51
  • 101

5 Answers5

2

move the declaration of the list to the top of the class and make a class variable...

List<PictureInfos> pictureInfos....
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I already tried that and this doesn't work. This variable has the same behavior than the final variable. – John Mar 18 '16 at 13:25
1
  1. Make it a class variable.
  2. Otherwise, instead of anonymous class; move your code into a separate class. Then pass the variable using constructor
Rusheel Jain
  • 843
  • 6
  • 20
0

Please try this

synchronized(this) {
        for (PictureInfos infos : pictureInfos) {
            if(!infos.exists()){
                infos.save();
            }
        }}

What I'm trying, is to first let insert a thread data, meanwhile block another.

Bills
  • 768
  • 7
  • 19
0

Maybe you have request twice and response twice,so thread is excuted twice.

-1

Maybe this is silly, but have you tried clearing the variable at the end of the for cycle?

...
for (PictureInfos infos : pictureInfos) {
    if(!infos.exists()) {
        infos.save();
    }
}
pictureInfos.clear();
...
cvfnv
  • 16
  • 4
  • This doesn't work. I see my problem and I don't understand. I tried to set two different list initialized in each thread and if I start this two thread at the same time, the data are mixed... Thread number 1 will get data of the second thread etc... – John Mar 18 '16 at 14:47
  • You have two threads because you get two calls on onResponse, right? Maybe you should render final your `Response response` parameter and move all code inside the run method ... – cvfnv Mar 18 '16 at 15:16
  • I already try this too and I don't understand why the thread crashes again :( – John Mar 18 '16 at 15:24
  • I think that the DBFlow method `.save()` don't like the multithreading... because it works well if once thread is started – John Mar 18 '16 at 15:35
  • Have you already checked this [link](http://stackoverflow.com/questions/36081015/android-sqlite-multi-threading-save-data-with-dbflow-retrofit) ? – cvfnv Mar 18 '16 at 15:38