Precautions when saving data in Realm (distinguishing between Managed / Unmanaged, when to use copyToRealm ())

Introduction

Shortly after I started using Realm, I was writing code without understanding the role of copyToRealm (), which caused a series of bugs. This is the article I want to read at that time.

Lesson: Understand the difference between Managed Object and Unmanaged Object and write your code

There are two patterns of Realm objects, ManagedObject and ʻUnmanagedObject`. In this regard, the official Realm documentation (https://realm.io/docs/java/latest/) (Realm Java 5.9.1) explains:

Instances of Realm objects can be either managed or unmanaged.

Managed objects are persisted in Realm, are always up to date and thread confined. They are generally more lightweight than the unmanaged version as they take up less space on the Java heap. Unmanaged objects are just like ordinary Java objects, they are not persisted and they will not be updated automatically. They can be moved freely across threads. It is possible to convert between the two states using Realm.copyToRealm and Realm.copyFromRealm.

Here is a slightly modified version of the above after Google Translate.

The instance of the Realm object can be either ** Managed ** (= managed) or ** Unmanaged ** (= unmanaged). ** ManagedObject ** is persisted to Realm, always kept up to date and thread restricted. They are generally lighter than ** UnmanagedObject ** because they take up less space on the Java heap. ** UnmanagedObject ** is exactly the same as a regular Java object. It is not persistent and will not be updated automatically. These can move freely between threads. You can switch between the two states by using Realm.copyToRealm and Realm.copyFromRealm.

The point is This means that you should use Realm.copyToRealm () as appropriate, as the value of ʻUnmanagedObject` is not updated automatically.

Implementation example: Saving ToDo object (2 ways)

For example, suppose you want Realm to manage the following classes:


public class ToDo extends RealmObject{
    //title
    public String title;
    //date
    public String updateDate;
    //Contents
    public String content;
    //Is the checkbox checked?
    public boolean isChecked;    
}

When implementing an object of this ToDo class ** first **, it can be either ManagedObject or ʻUnmanagedObject`.

                //Implemented as a Managed Object
                ToDo todo = realm.createObject(ToDo.class);

                //Implemented as Unmanaged Object
                ToDo todo = new ToDo();

Because, as mentioned in the quote in the official document earlier, if you want to change ʻUnmanagedObject to ManagedObjectin the middle of the process, you can solve it by usingRealm.copyToRealm ()and changeManagedObject. If you want to change it to ʻUnmanagedObject, you can solve it by usingRealm.copyFromRealm ().

However, most Realm introductory articles implement it as ManagedObject. (Self-examination)

ToDo object save method

When saving the value of an object, it is necessary to distinguish whether the object is ManagedObject or ʻUnmanagedObject` and write the process.

As an example, we have prepared a ToDo object save method save ().

First, take a look at the following framework.

save method framework


    public void save(final String title, final String updateDate, final String content){
        realm.executeTransaction(new Realm.Transaction(){
            @Override
            public void execute(Realm bgRealm){

                //Write the process to save the value here.

            }
        });
    }

In this framework, it is necessary to write the processing according to the state of the object.

When saving a Managed Object

The value of ManagedObject is updated automatically, so you can do the same as a normal Java object.

Processing within the framework


                ToDo todo = realm.createObject(ToDo.class);
                //Substitute the value you want to save
                todo.title = title;
                todo.updateDate = updateDate;
                todo.content = content;

When saving an Unmanaged Object

The value of ʻUnmanagedObject is not updated automatically, so you need to useRealm.copyToRealm () `at the end.

Processing within the framework



                ToDo todo = new ToDo();
                //Substitute the value you want to save
                todo.title = title;
                todo.updateDate = updateDate;
                todo.content = content;
                //Change from Unmanaged Object to Managed Object and copy to Realm
                realm.copyToRealm(todo);

A word summary

When using Realm, write your code paying attention to whether the object is Managed or ʻUnmanaged`.

Recommended Posts

Precautions when saving data in Realm (distinguishing between Managed / Unmanaged, when to use copyToRealm ())
How to fix a crash when deleting Realm data in Swift UI List
[Swift] Use UserDefaults to save data in the app
Use collection_select to pull down the data stored in Active_Hash
How to use JSON data in WebSocket communication (Java, JavaScript)