Actually in Realm, the class name of the model coincides with the name of the table. And how to create a lot of tables with different names but on 1 model? That is, let's say there is a list of brands of shoes, this is 1 table. Each brand has a collection (there are as many of these tables as brands, although there may even be more, but we are not distracted). Each model in the shoe collection has id, color, price .. . (which do not depend on the collection or brand of shoes) let's say you need to get JSON parsed and saved in a table.

1. List of brands

2. collection_dolce

3. collection_eccet

............................

N. collection_xxx

How to create these tables with different names on one model? In the list of brands, you can add a column with the names of collection tables.

  • Only yesterday I asked myself the same question :) PS: stackoverflow.com/questions/40542762/… - post_zeew
  • Did you understand how to do it in a casual way? I have something not to the end. - Pavel Zlotarenchuk
  • An object creates an additional field. The name of the "table" is recorded in this field. Further, when sampling, simply indicate the desired "table". - post_zeew
  • In general, noSQL DB, as Realm for this type of storage is not the best choice. Here the classical relational database is most appropriate, as SQLite and tables should be: brands - brand names, collections - collections names with a bundle for brands and models, models - specific models data with a bundle for brands and collections. - pavlofff
  • Can you explain why you think so? I'm just literally using the second realm, maybe really wrong. - Pavel Zlotarenchuk

1 answer 1

How to create these tables with different names on one model?

As far as I know, this is impossible out of the box, but there are several solutions , for example:

Create a field in the class:

@PrimaryKey String mTableName; 

Further, for grouping objects we use this field.

Getting objects from a specific group:

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

PS: The solution is not very good, but still.

PPS: I didn’t really read the initial task, but, it seems, a good solution would be to create several tables with a One-to-many link.