简体   繁体   中英

Realm Database Multiple Tables

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

Some points:

Each realm config will be converted to one database. Each class which extends realmobject will be converted to one table. Each instance of these classes will be converted to one row(if you save it).

First of all try to decide how many databases and tables do you need? (99 percents of applications use 1 database). You can read and follow database normalization methods and then try to impelement what you need with Realm.

As I know a realm is a like Table, but it can store multiple Models

1 Realm is 1 database file, and it can store multiple RealmModels.

Every RealmModel corresponds to 1 "table".

PS. I found this answer as well but it doesn't feel right either:

That's because that answer is for storing subtypes of a given class in the same table. Although technically it is possible even in your setup.

public class Playlist extends RealmObject {
    @Index
    private String type; // SONGS_NEW, SONGS_BEST, PLAYLISTS_BEST, PLAYLISTS_MINE, PLAYLISTS_OTHERS, ALBUMS_SPECIAL, NEWS, FEED

    private RealmList<Song> songs;
}

And then

public RealmResults<Song> getSongsForType(Realm realm, RealmType type) {
    return realm.where(Playlist.class).equalTo("type", type.name())
                .findFirst().getSongs().where().findAll();
}

I'm new to Realm too and switching from SQL Lite and I was wondering how to replace tables since what I read about Realm is that it stores objects. After downloading Realm Studio I kinda figured out how Realm stores the data.

This is what I see and how you can have multiples tables for your situation.

When you create Realm Object it basically creates a table with fields based on objects properties key. So you can't have to table of same objects or its just useless since they are the same

For your case, you need only one table for Songs and then have other tables (songs_new and songs_best ) and these tables would contain a key that refers to an item in the songs_table basically a foreign key.

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