I got confused about the concept of tables and realm although I read the Realm documentation. As I know a realm is a like Table, but it can store multiple Models. The problem is, I want to have multiple tables (some of them just contain objects of 1 class, and some multiple classes) but I don't know how to manage multiple realms. I want my "Database" class to be a wrapper for RealmDatabase and I want multiple Tables in it, for example, a table for "new songs" and a table for "best songs". So what I did was:
public class Database {
//tables that I want to have
public enum RealmType {
SONGS_NEW, SONGS_BEST, PLAYLISTS_BEST, PLAYLISTS_MINE, PLAYLISTS_OTHERS, ALBUMS_SPECIAL, NEWS, FEED
}
private static final String TAG = "Database";
Context context;
//A realm object for each table
Realm profileRealm;
Realm songs_newRealm;
Realm songs_bestRealm;
//and so on...
// as I didn't know how to differentiate between Realms (all of them are created with Realm.getDefaultInstance(), and I think they are just one Realm. so I created a class for different configs )
private static class RealmConfigs {
static RealmConfiguration songs_newConfig;
static RealmConfiguration songs_bestConfig;
//and so on..
public RealmConfigs() {
config();
}
private static void config() {
songs_newConfig = new RealmConfiguration.Builder()
.name("songs_new.realm")
.deleteRealmIfMigrationNeeded()
.build();
songs_bestConfig = new RealmConfiguration.Builder()
.name("songs_best.realm")
.deleteRealmIfMigrationNeeded()
.build();
}
}
}
But it just doesn't feel right. I know that I'm confused in Realm's core concepts but the documentation just didn't help either. A clear explanation on how to correctly manage different Tables and initialize them would be appreciated.
PS. I found this answer as well but it doesn't feel right either: https://stackoverflow.com/a/40547703/7893941