【发布时间】:2020-02-21 21:45:58
【问题描述】:
虽然我阅读了 Realm 文档,但我对表和领域的概念感到困惑。 据我所知,领域是一个类似的表,但它可以存储多个模型。 问题是,我想要多个表(其中一些只包含 1 个类的对象,一些包含多个类)但我不知道如何管理多个领域。 我希望我的“数据库”类成为 RealmDatabase 的包装器,并且我希望其中包含多个表,例如,一个用于“新歌曲”的表和一个用于“最佳歌曲”的表。所以我所做的是:
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();
}
}
}
但就是感觉不对劲。我知道我对 Realm 的核心概念感到困惑,但文档也无济于事。关于如何正确管理不同的表和初始化它们的清晰解释将不胜感激。
PS。我也找到了这个答案,但感觉也不对: https://stackoverflow.com/a/40547703/7893941
【问题讨论】:
标签: android realm android-database