Realm (Java) database using a document (mode Schemas)

Realm (Java) database using the document (directory)

Realm of the default schema just all Realm model class project. However, you can change this behavior - for example, you might want to limit the Realm contains only a subset of the class. To do this, create a custom RealmModule .

// 创建module
@RealmModule(classes = { Person.class, Dog.class })
public class MyModule {
}

// 在RealmConfiguration中将module设置为仅允许该module定义的类。
RealmConfiguration config = new RealmConfiguration.Builder()
  .modules(new MyModule())
  .build();

// 可以将多个module组合成一个模式。
RealmConfiguration config = new RealmConfiguration.Builder()
  .modules(new MyModule(), new MyOtherModule())
  .build();

For library developers: it contains the Realm of the library must be RealmModuleopen and use its architecture. This will prevent the formation of default for the library project RealmModule, which will use the default RealmModule App conflict. Library of RealmModulethe way their Realm class library is also open to the application.

// 库必须创建一个module并设置library = true。 这将阻止创建默认module。
// 可以使用allClasses = true来代替列出库中的所有类。
@RealmModule(library = true, allClasses = true)
public class MyLibraryModule {
}

// 因此,需要图书馆项目明确设置自己的module。
RealmConfiguration libraryConfig = new RealmConfiguration.Builder()
  .name("library.realm")
  .modules(new MyLibraryModule())
  .build();

// 应用程序可以将库RealmModule添加到其自己的架构中。
RealmConfiguration config = new RealmConfiguration.Builder()
  .name("app.realm")
  .modules(Realm.getDefaultModule(), new MyLibraryModule())
  .build();

A file can not have more RealmModule statement. If you have two or more RealmModule, you must declare into multiple files, each file has only one statement.

Check here about RealmModuleshow it works between the library and the application project complete example .

Published 59 original articles · won praise 88 · views 190 000 +

Guess you like

Origin blog.csdn.net/geofferysun/article/details/105102695