为什么 Room observer 会一遍又一遍地将同一个实体添加到数据库中?

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

我检索了我使用 Retrofit 创建并上传到互联网的 JSON 数据。我可以成功检索和显示数据,但是当我将数据插入 Room 数据库时,只添加了最后一个 JSON 对象,似乎有 JSON 个对象.

根据我的 logJSON 数据在 Retrofits onResponse() 方法中完整检索 - 没问题。但是根据 RoomgetAllData() 上的 observer 方法,只有最后一个 JSON 对象被添加到 Room。观察者被触发,每个 JSON 个对象触发一次,但每次只显示最后一个 JSON 个对象。我使用 Ridill SQLlte 仔细检查了数据库文件,它证实了这一点:

我的 Log 看起来像这样:

//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'}

onResponse() 方法如下所示:

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 );

                    }
            }
        }

我的 observer 看起来像这样:

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());
                }
            }
        }
    } );

onResponse() 方法中一切看起来都很好,那么为什么数据没有正确添加到 Room 中?如何将所有 JSON 对象放入 Room 而不仅仅是最后一个?我在这里错过了什么?

在此先感谢您的帮助!

您不想重复使用您的房间实体对象。移动

MyRoomEntity myRoomEntity = new MyRoomEntity();

到填充循环内部

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 );
}