2

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

MH. Abdi
  • 298
  • 5
  • 22

3 Answers3

3

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.

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30
  • Thanks , that was very clear :) but what if I want to have multiple tables of my Song class? – MH. Abdi Aug 07 '18 at 20:35
  • Why do you want to seperate your songs tables? Do they have different column? If they are totally different, you can make 2 seperate classes for that. If they are dependant try to connect them with foreign key and if they are the same concept but have differnet value try to use one table. – Ehsan Mashhadi Aug 07 '18 at 20:40
  • They are the same class, but they have different usage; They will be shown in the different parts of the app. And I dont know how to differentiate them with queries! (I dont think it possible without adding another field called type) – MH. Abdi Aug 07 '18 at 20:45
  • What I underatood from your code is you need to have one song table with type column to categorize them. – Ehsan Mashhadi Aug 07 '18 at 20:47
  • Sure, I thought that is an unusual way to handle it! Damet garm though :) – MH. Abdi Aug 07 '18 at 20:58
  • I was considering using `type` column in `Song` table, but I think he must be able to support storing the same song in different playlists, In that case, he would need to build a primary key from `type_songid` and create duplicate song entries per playlist the song is stored in. But it *is* easier to query, so that also makes sense in a way. However, this is why i was considering creating an actual `RealmList` to store the order of items. – EpicPandaForce Aug 07 '18 at 23:05
  • @Apptic If you think my answer helped you please accept the answer. – Ehsan Mashhadi Aug 08 '18 at 06:09
  • sure :) but I'd suggest updating your answer so it contains adding the "type" field and compound primary key (tablename+id) – MH. Abdi Aug 08 '18 at 07:02
1

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();
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

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.

JPilson
  • 1,075
  • 11
  • 10