How to exclude a Realm model class from a specific Realm file in DB scheme?

NickUnuchek :

Realm version:

classpath "io.realm:realm-gradle-plugin:5.8.0"

I found that I should add modules to io.realm.RealmConfiguration. So I made this:

RealmConfiguration.Builder builder = new RealmConfiguration.Builder()
            .deleteRealmIfMigrationNeeded()
            .schemaVersion(1)
            .encryptionKey(/*my encryption key*/)
            .name("pay_card.realm");

        builder.addModule(new RPayCardRealmModule());

    return Realm.getInstance(builder.build());

RPayCardRealmModule.class

@RealmModule(library = true,classes = {
        RPayCard.class
})
public class RPayCardRealmModule {
}

RPayCard.class

@lombok.Getter
@lombok.Setter
@lombok.ToString(includeFieldNames = false)
@lombok.Builder
@lombok.AllArgsConstructor
@lombok.NoArgsConstructor
@lombok.EqualsAndHashCode(of = "id")
public class RPayCard extends RealmObject {
    @io.realm.annotations.PrimaryKey
    @io.realm.annotations.Required
    String id;

    private String billNumber;
    private String cardNumber;
    private String cardExpirationDate;
    private String cardType;
    private String cardHolderName;
    private boolean selected;

}

But when I look into the db file "pay_card.realm".

It looks like that. Why other classes are added to db scheme? How to exclude them? enter image description here

haroldolivieri :

From RealmModule documentation

Realms default behavior is to automatically create a RealmModule called DefaultRealmModule which contains all classes extending RealmObject in a project. This module is automatically known by Realm.

From builder.addModule() documentation

FIXME: Temporary visible DEBUG method. Will add a module unconditionally. Adds a module to already defined modules.

From RealmConfiguration.Builder modules documentation

Replaces the existing module(s) with one or more RealmModules. Using this method will replace the current schema for this Realm with the schema defined by the provided modules. A reference to the default Realm module containing all Realm classes in the project (but not dependencies), can be found using Realm.getDefaultModule(). Combining the schema from the app project and a library dependency is thus done using the following code: builder.modules(Realm.getDefaultMode(), new MyLibraryModule());

TLDR; You should use RealmConfiguration.Builder.modules() to clear modules list at first instead builder.addModule(new RPayCardRealmModule())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130916&siteId=1