简体   繁体   中英

Why does Room observer add the same entity over and over again into the database?

I retrieve JSON data that I have created and uploaded to the internet using Retrofit . I can successfully retrieve and display the data, but when I insert the data into Room database, only the very last JSON object is added, as many times as there are JSON objects it seems.

According to my log the JSON data is retrieved in whole in Retrofits onResponse() method - no problem there. But according to the observer method on the getAllData() from Room only the very last JSON object is added to Room . The observer is triggered, once for every JSON object, but every time, only the last JSON object is displayed. I have double checked the data base file using Ridill SQLlte and it confirms this:

在此处输入图片说明

My Log looks like this:

//First observer shows nothing

D/TAG: OBSERVED

//JSON is retrieved, looks fine

D/TAG: JSON GATHERED
D/TAG: ROOM NAME: Bob
D/TAG: ROOM DATE: 3/13/2015
D/TAG: ROOM FROM: 8:00
D/TAG: ROOM UNTIL: 13:00
D/TAG: ROOM NAME: Joe
D/TAG: ROOM DATE: 1/3/2015
D/TAG: ROOM FROM: 12.30
D/TAG: ROOM UNTIL: 13:00
D/TAG: ROOM NAME: Martin
D/TAG: ROOM DATE: 1/5/2015
D/TAG: ROOM FROM: 15.30
D/TAG: ROOM UNTIL: 16:00

//Observer is set again, shows last of the JSON objects

D/TAG: OBSERVED
D/TAG ROOM: MyRoomEntity{id=1, name='Martin', date='1/5/2015', from='15.30', until='16:00'}

//Observer fires again, now having added the last JSON object a second time

D/TAG: OBSERVED
D/TAG ROOM: MyRoomEntity{id=1, name='Martin', date='1/5/2015', from='15.30', until='16:00'}
D/TAG ROOM: MyRoomEntity{id=2, name='Martin', date='1/5/2015', from='15.30', until='16:00'}

//Observer fires again, now adding the last object for a third time.

D/TAG: OBSERVED
D/TAG ROOM: MyRoomEntity{id=1, name='Martin', date='1/5/2015', from='15.30', until='16:00'}
D/TAG ROOM: MyRoomEntity{id=2, name='Martin', date='1/5/2015', from='15.30', until='16:00'}
D/TAG ROOM: MyRoomEntity{id=3, name='Martin', date='1/5/2015', from='15.30', until='16:00'}

The onResponse() method looks like this:

public void onResponse(Call<List<RetrofitVariables>> call, Response<List<RetrofitVariables>> response) {
            List<RetrofitVariables> myResponse = response.body();
            MyRoomEntity myRoomEntity = new MyRoomEntity();

            Log.d( "TAG" , "JSON GATHERED" );

            if(myResponse != null) {
                MyRoomDatabase db = myViewModel.getRoomDatabase();

                    for(RetrofitVariables item: myResponse) {

                        Log.d( "TAG" , "name: " + item.getName());
                        Log.d( "TAG" , "from: " + item.getFrom());
                        Log.d( "TAG" , "until: " + item.getUntil());
                        Log.d( "TAG" , "Counter: " + counter);

                        myRoomEntity.setName( item.getName() );
                        myRoomEntity.setDate( item.getDate() );
                        myRoomEntity.setFrom( item.getFrom() );
                        myRoomEntity.setUntil( item.getUntil() );

                        //THESE LOGS SHOW CORRECT RESULT
                        Log.d( "TAG" , "ROOM NAME: " + myRoomEntity.getName());
                        Log.d( "TAG" , "ROOM DATE: " + myRoomEntity.getDate());
                        Log.d( "TAG" , "ROOM FROM: " + myRoomEntity.getFrom());
                        Log.d( "TAG" , "ROOM UNTIL: " + myRoomEntity.getUntil());


                        myViewModel.insertDataVM( myRoomEntity );

                    }
            }
        }

And my observer looks like this:

myViewModel.getAllDataVM().observe( this, new Observer<List<MyRoomEntity>>() {
        @Override
        public void onChanged(@Nullable List<MyRoomEntity> myRoomEntities) {
            //myAdapter.setList( myRoomEntities );

            Log.d("TAG", "OBSERVED");
            if(myRoomEntities != null) {
                for(MyRoomEntity item: myRoomEntities) {
                    Log.d("TAG ROOM ", "" + item.toString());
                }
            }
        }
    } );

Everything looks fine in the onResponse() method, so why arn't the data added properly into Room ? How do I get all the JSON objects into Room and not just the last one? What am I missing here?

Thanks in advance for any help!

You don't want to reuse your room entity object. Move the

MyRoomEntity myRoomEntity = new MyRoomEntity();

To inside the populate loop

for(RetrofitVariables item: myResponse) {
    MyRoomEntity myRoomEntity = new MyRoomEntity();
    myRoomEntity.setName( item.getName() );
    myRoomEntity.setDate( item.getDate() );
    myRoomEntity.setFrom( item.getFrom() );
    myRoomEntity.setUntil( item.getUntil() );

    myViewModel.insertDataVM( myRoomEntity );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM