Not found on the official website, something similar. The task is this - there is a large list of goods - this is the main base, some goods are taken from it and purchases are created, in the form of additional lists, and there can be a great many. How can I organize the work of Realm for additional lists, each time create a new Realm object or is there some other way? I ask for help.

    1 answer 1

    1. Statically initialize RealmConfigurations , it’s best to do this in the OnCreate Application. Thus, you will have a single configuration per application.
    2. Create a class that will have an object of type Realm , and implemented according to the DAO pattern - it will have CRUD operations that will be performed through transactions of the type:

      public void create(T model) { realm.beginTransaction(); model.setKey(getDAOKey()); realm.copyToRealm(model); realm.commitTransaction(); }

    3. In this case, T is the object with which we work - the object is our table, T inherits from RealmObject .

      abstract public class BaseDao<T extends RealmObject>

      Thus, in this class, you can register any method you need, without duplicating an object of type Realm .

    Full example:

      abstract public class BaseDao<T extends RealmObject> { private Realm realm; DatabaseConfiguration databaseConfiguration; public BaseDao() { databaseConfiguration = DatabaseConfiguration.getInstance(); databaseConfiguration.setRealmConfiguration(); this.realm = databaseConfiguration.getRealmInstance(); } public void create(T model) { realm.beginTransaction(); model.setKey(getDAOKey()); realm.copyToRealm(model); realm.commitTransaction(); } }