如何将RealmObject添加到RealmList?

时间:2018-07-28 16:31:24

标签: realm

我正在尝试像这样将SongRealm添加到SongListRealm:

public void insertSongToListWithKey(final int key, Song song){
    final SongRealm songRealm = mapper.toRealm(song);
    realm.beginTransaction();

    SongListRealm songListRealm = realm.where(SongListRealm.class).equalTo("key", key).findFirst();
    if(songListRealm!=null){
        songListRealm.getSongList().add(songRealm);
        realm.insertOrUpdate(songListRealm);
    }

    realm.commitTransaction();
}

之前:

  

名为“ A”的歌曲列表{名为“ 1”的歌曲,名为“ 2”的歌曲}

     

名为“ B”的歌曲列表{名为“ 14”的歌曲}

之后:

  

名为“ A”的歌曲列表{名为“ 4”的歌曲,名为“ 4”的歌曲}

     

名为“ B”的歌曲列表{名为“ 4”的歌曲,名为“ 4”的歌曲}

我在做什么错?祝你有个美好的夜晚。预先谢谢你。

编辑

我要添加更多代码:)

SongListMapper(在SongList上更改SongListRealm的类)方法:

public SongList fromRealm(SongListRealm songListRealm){
    SongList songList = new SongList();
    ArrayList<Song> songArrayList = new ArrayList<>();
    RealmList<SongRealm> realmSongList = songListRealm.getSongList();
    for(SongRealm songRealm: realmSongList){
        songArrayList.add(songMapper.fromRealm(songRealm));
    }
    songList.setSongList(songArrayList);
    songList.setTitle(songListRealm.getName());
    songList.setKey(songListRealm.getKey());
    return songList;
}

SongMapper(用于在Song上更改SongRealm的类,反之亦然):

public Song fromRealm(SongRealm songRealm){
    Song song = new Song();
    song.setArtist(songRealm.getArtist());
    song.setId(songRealm.getId());
    song.setPath(songRealm.getPath());
    song.setTitle(songRealm.getTitle());
    return song;
}

getAllTheSongLists()方法:

public ArrayList<SongList> getAllTheSongLists() {
    ArrayList<SongList> songs = new ArrayList<>();
    SongListMapper mapper = new SongListMapper();
    RealmResults<SongListRealm> all = realm.where(SongListRealm.class).findAll().sort("key");
    for (SongListRealm songRealm : all) {
        if(songRealm.getKey()==idLastSongListValue)
            continue;
        songs.add(mapper.fromRealm(songRealm));
    }
    return songs;
}

我要用idLastSongListValue变量中的键保存上次使用的SongList-因此它不是由用户创建的SongList,并且我不想为用户显示它。

[已解决]

我必须使用新的主键创建新对象,并将Song对象的变量设置为创建的SongRealm对象,并从EpicPandaForce复制方法。希望对您有帮助。

public void insertSongToListWithKey(final int key, final Song song){
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm r) {
            SongRealm songRealmNew = createSongRealm(song);
            SongListRealm songListRealm = realm.where(SongListRealm.class).equalTo("key", key).findFirst();
            if (songListRealm != null) {
                SongRealm savedSongRealm = realm.copyToRealmOrUpdate(songRealmNew);
                RealmList<SongRealm> songs = songListRealm.getSongList();
                if(!songs.contains(savedSongRealm)) {
                    songs.add(savedSongRealm);
                }
            }
        }
    });
}

private SongRealm createSongRealm(Song song){
    SongRealm songRealmNew = realm.createObject(SongRealm.class, generateIdForSong());
    songRealmNew.setId(song.getId());
    songRealmNew.setTitle(song.getTitle());
    songRealmNew.setPath(song.getPath());
    songRealmNew.setArtist(song.getArtist());
    return songRealmNew;
}

 private int generateIdForSong() {
    if(realm.where(SongRealm.class).max("key")==null)
        return 0;
    return realm.where(SongRealm.class).max("key").intValue() + 1;
 }

1 个答案:

答案 0 :(得分:1)

通常应该是这样的:

public void insertSongToListWithKey(final int key, Song song){
    final SongRealm songRealm = mapper.toRealm(song);
    realm.executeTransaction((r) -> {
        SongListRealm songListRealm = realm.where(SongListRealm.class).equalTo("key", key).findFirst();
        if(songListRealm != null) {
            SongRealm savedSongRealm = realm.copyToRealmOrUpdate(songRealm);
            RealmList<SongRealm> songs = songListRealm.getSongList();
            if(!songs.contains(savedSongRealm)) {
                songs.add(savedSongRealm);
            }
        }
    });
}