3

I have a RealmObject Class. I want multiple tables of the same class. Can I do that in a single Realm(Database) or I need to have multiple Realms(One table per each Realm).

If it has to be multiple Realms, how fast is the Realm switching process?

Anoop
  • 993
  • 6
  • 16
  • Do you have a specific number of this table, or `N` of this table? – EpicPandaForce Nov 11 '16 at 07:09
  • BTW please read [this article, part `“I don’t really need polymorphism, I just need multiple classes that slightly differ from one another, but share some of the same elements”`](https://medium.com/@Zhuinden/designing-the-schema-of-realm-effectively-and-other-realm-tips-feb76c5b6072#.oah921cgf) – EpicPandaForce Nov 11 '16 at 07:10
  • @EpicPandaForce Nope, the number of tables are dynamic. Say its like a channel of information. More and more channel will be added over time. Only thing is information coming though these channels are of same type. But there are some constraints that they should be stored separately. – Anoop Nov 11 '16 at 07:24
  • You could store which table this should be with a string discriminator field. – EpicPandaForce Nov 11 '16 at 07:26
  • @EpicPandaForce That I have seen on Realm website. DynamicRealms. But still want to check whether is there any better ways to do it. – Anoop Nov 11 '16 at 07:27
  • Not what I meant. I said typed realm and discriminator field. But if you desperately want many number of dynamic tables, then DynamicRealm creates dynamic schema. – EpicPandaForce Nov 11 '16 at 07:29
  • If the number of classes you work with are dynamic, as it can change while the app is running, you need to use `DynamicRealm`. `Realm` expects a schema to stable for that app version, and if it changes a migration is required. – Christian Melchior Nov 11 '16 at 08:29
  • @ChristianMelchior I believe schema is going to the same. Say I have a class called Data. I need to store it like Data for A, Data for B, Data for C etc. Thing is the contents (fields) of Data are going to be same for all. Only things is they have to be stored separately. – Anoop Nov 11 '16 at 09:13
  • In that case, storing them in each Realm is fine. There is no penalty for switching between Realms as they are completely seperate. – Christian Melchior Nov 11 '16 at 10:57
  • @ChristianMelchior Sorry for coming back to this after a long time. When you say there is no penalty for switching Realms, does that mean that it will not shoot up memory usage and switching time is also very fast? – Anoop Feb 15 '17 at 05:38
  • @ChristianMelchior I am having the same difficulties while creating multiple table for the same type of model class. Please help if you find any..!! – Abhishek Jha Feb 24 '17 at 14:02
  • @AbhishekJha I have done that and working fine for me. I didn't get enough time to test it extensively, but so far no issues. What I have done is created different realm for each table (realmObject class) – Anoop Feb 25 '17 at 07:18
  • @AnoopSS Thanks for responding!! but the problem is: i have made my app with the scenario of having multiple api call response to be handled by the same model class with different set of data. So i am a little afraid of changing that sort of model classes for every individual Api calls and storing them as you suggested. It would be appreciable if you could get me something that i can use to make it work with my present scenario. Looking forward to ur precious comments... – Abhishek Jha Feb 26 '17 at 12:36
  • @AbhishekJha Sorry for the delayed reply. In my case also its also the model is same and which is used to store data of different people and one business case I have to follow is for each person data should be stored separately. That means there should be a separate DB for each user. In my case model name is stat. I will create a realm A for person A which will have stat table, similarly realm B for Person B, realm C etc. In all those realms the model used is stat. Depending on the user on focus i will switch realms – Anoop Mar 01 '17 at 10:53

1 Answers1

2

I want multiple tables of the same class.

Well you have two reasonable options:

1.) add a discriminator field that determines which "table" this current object instance belongs to

public class MyObject extends RealmObject {
    @PrimaryKey
    private String tableAndId;

    @Index
    private String table;

    @Index
    private long id;

    // getters, setters
}

Then you can query "per table":

RealmResults<MyObject> results = realm.where(MyObject.class)
                                      .equalTo(MyObjectFields.TABLE, "tableA")
                                      .findAll();

2.) create any table you want dynamically using DynamicRealm and manual creation of the schema via the RealmSchema.

DynamicRealm dynamicRealm = DynamicRealm.getInstance(config);
RealmSchema schema = dynamicRealm.getSchema();
RealmObjectSchema objectSchema;
if(!schema.contains("MyObject_Table_A")) {
    objectSchema = schema.create("MyObject_Table_A");
    objectSchema.addField("id", long.class, FieldAttribute.PRIMARY_KEY);
    objectSchema.addField("created", Date.class, FieldAttribute.INDEXED);
    //...
} else {
    objectSchema = schema.get("MyObject_Table_A");
}

And then you can write into the dynamic realm:

dynRealm.executeTransaction(new DynamicRealm.Transaction() {
    @Override
    public void execute(DynamicRealm dynRealm) {
        DynamicRealmObject dynObj = dynRealm.where("MyObject_Table_A")
                                            .equalTo("id", id)
                                            .findFirst();
        if(dynObj == null) {
            dynObj = dynRealm.createObject("MyObject_Table_A", id);
        }
        dynObj.setDate("created", new Date());
    }
});

You can also have a new configuration per table, but that sounds like an unreasonable option, so I barely even want to mention it.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428