Hot to use Realm in android project.
Befor you use realm we should be read documentation
https://realm.io/docs/java/latest#getting-started
Then we got the way how to install.
Install Realm as a Gradle plugin.
Setp 1:
change build.gradle in project levelbuildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.realm:realm-gradle-plugin:5.14.0"
}
}
Step 2:
Apply the realm-android plugin to the top of the application level build.gradle file.Create reaml map Object
There has two way for mapping Object
- Make
Object
extendsReamlObject
- Or make
Object
implementReamlModel
Declear reaml mapping objects
Realm not support
data class
yet. So we can't use it now. And object will make it open, because Realm object will generate subclass after aapt
or kapt
.// 1.
open class User : RealmObject() {
var id: String = UUID.randomUUID().toString()
var name: String = ""
}
// 2.
@RealmClass
open class User : RealmModel {
var id: String = UUID.randomUUID().toString()
var name: String = ""
}
Initializing Realm
Before you can use Realm in your app, you must initialize it. This only has to be done once.
Realm.init(context);
You must provide an Android context. A good place to initialize Realm is in onCreate on an application subclass:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
}
}
If you create your own application subclass, you must add it to the app’s
AndroidManifest.xml
:<application
android:name=".MyApplication"
...
/>
Configuring a Realm
After initializ then we need config realm.
Config can set with file will save data.
RealmConfiguration config = new RealmConfiguration.Builder().build();
That configuration—with no options—uses the Realm file default.realm located in Context.getFilesDir. To use another configuration, you would create a new RealmConfiguration object:
// The RealmConfiguration is created using the builder pattern.
// The Realm file will be located in Context.getFilesDir() with name "myrealm.realm"
RealmConfiguration config = new RealmConfiguration.Builder()
.name("myrealm.realm")
.encryptionKey(getKey())
.schemaVersion(42)
.modules(new MySchemaModule())
.migration(new MyMigration())
.build();
// Use the config
Realm realm = Realm.getInstance(config);
You can have multiple
RealmConfiguration
objects, so you can control the version, schema and location of each Realm independently.RealmConfiguration myConfig = new RealmConfiguration.Builder()
.name("myrealm.realm")
.schemaVersion(2)
.modules(new MyCustomSchema())
.build();
RealmConfiguration otherConfig = new RealmConfiguration.Builder()
.name("otherrealm.realm")
.schemaVersion(5)
.modules(new MyOtherSchema())
.build();
Realm myRealm = Realm.getInstance(myConfig);
Realm otherRealm = Realm.getInstance(otherConfig);
Also in default config with default instance.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// The default Realm file is "default.realm" in Context.getFilesDir();
// we'll change it to "myrealm.realm"
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().name("myrealm.realm").build();
Realm.setDefaultConfiguration(config);
}
}
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Realm realm = Realm.getDefaultInstance(); // opens "myrealm.realm"
try {
// ... Do something ...
} finally {
realm.close();
}
}
}
Now we can access
Realm
with configration.val realmInstance = Realm.getInstance(YourConfiguration)
// use instance for queries
// insert or update
// Attention change data must be in transactional.
realmInstance.use {
// for insert in transactional.
}
评论
发表评论