Java Realm is difficult to handle 3 points

Realm is generally useful, although it's awkward to handle.

As a premise, I found it awkward to handle mainly when using Realm as a simple DB. Especially compared to the Swift version, it seems to be difficult to handle.

Unit tests cannot be completed with just the JVM

Realm depends on android, so I have to run it on Android Test. In that case, you can use Robolectric, but it seems that Realm is not yet supported (https://github.com/realm/realm-java/issues/904).

That is, it takes a long time to run, which makes testing uncomfortable.

Of course, it can work with PowerMock, but I want to verify that insert, update, select is working properly in realm with a test targeting Dao. (In layers other than Dao, Dao is mocked, so there is no problem.)

In Official Samples, PowerMock is still available. I am using.

Currently, there is no fundamental solution, it seems that you can only test if the mock is calling the method correctly.

Crash when accessed after close

It's natural to open and close a resource, but when I access a property with the intention of putting it in an entity, it crashes (`` This Realm instance has already been closed```. Java Realm is a specification that cannot be accessed after closing a resource.

To avoid that, you have to handle everything within the transaction. If you want to complete it with Dao only, you need to copy it to an instance not managed by realm as follows.

    fun find(): AuthEntity? {
        var result: AuthEntity? = null
        Realm.getDefaultInstance().use { realm ->
            val entity = realm.where(AuthEntity::class.java).findFirst()
            if (entity != null) {
                result = realm.copyFromRealm(entity)
            }
        }
        return result
    }

Realm is generally configured to be instantiated with onCreate () `` `and closed with onDestroy () `` ([Official Document](https://realm.io/). docs / java / latest /) also seems to be "closed with ʻonDestory () ``).

But you don't want the Activity to depend on the Dao element, and you don't want the ViewModel to depend on the Realm, for example in the MVVM. So, I think I will create a class that wraps Realm and implement it to eliminate the dependency. However, it is exaggerated to be aware of transactions without depending on Realm even for simple acquisition processing.

Therefore, I think there is a decision / solution to write the above code. However, this process seems to be costly in memory and processing time.

Entity needs Public default constructor

When using with Kotlin, you have to put a default value in the argument due to this constraint. If you use kotlin-noarg Plugin, the default constructor will be generated automatically, but in the case of Realm, it could not be built because it was played in the pre-verification. Corresponding source locationis.

So even if you don't need the default values, you would write a class like this.

@RealmClass
data class SampleEntity(
        @PrimaryKey
        @SerializedName("id")
        var id: Int = 0,

        @SerializedName("sample")
        var sample: String = ""
) : RealmModel 

Recommended Posts

Java Realm is difficult to handle 3 points
Difficult to handle minimum values for Java8 LocalDateTime and Java6 Date
[Java] Points to note with Arrays.asList ()
[java] Summary of how to handle char
[Java] Map # merge is hard to understand.
Write code that is difficult to test
[java] Summary of how to handle character strings
Points to review when Rubocop is slow to run
What is java
What is Java <>?
What is Java
[Java] Introduction to Java
Introduction to java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
What is Java Encapsulation?
Kotlin's improvements to Java
What is Java technology?
About Java entry points
What is Java API-java
Introduction to java command
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
To you who suffer from unexpected decimal points in Java
How to handle exceptions coolly with Java 8 Stream or Optional
Differences in how to handle strings between Java and Perl
Road to Java Engineer Part2 What kind of language is Java?
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[Java] Code that is hard to notice but terribly slow
[Java] UTF-8 (with BOM) is converted to 0xFFFD (REPLACEMENT CHARACTER)