Original link: up and Running with realms for Android
Introduced
If you're concerned about the latest developments in Android, you'll certainly hear about realm, a lightweight database that can replace SQLite and ORM in Android development. Framework.
Realm is faster than SQLite and has many advanced features, such as support for JSON, fluent APIs, notification of data Changes (viewer), encryption support ... All of this will make Android developers live a more natural life (this is nonsense, the developer of the day can be smart, of course, it may be different at home and abroad ...).
The subject of this article is realm for Android, and in this article I will use realm v0.84.1.
Add Realm to your project
To use realm in an Android project, you need to add the following code to the module's Build.gradle file:
‘io.realm:realm-android:0.84.1‘
Create a Realm
A realm and SQLite are more similar. It has a file associated with him and this file is created once and will permanently exist on the Android system.
To create a realm, you can Activity call a static method in either Realm.getInstance .
Realm myRealm = Realm.getInstance(context);
It is important to note that we created it so that it was not specified RealmConfiguration , and the file created at this time uses the default filename, default.realm.
If you need to create another realm, you have to pass it on and RealmConfiguration.Builder give it a unique name.
Realm myOtherRealm = Realm.getInstance( new RealmConfiguration.Builder(context) .name("myOtherRealm.realm") .build());
Create Realmobject
If a javabean inherits RealmObject , then it can be used to store realms, and if you want to know what JavaBean is, here's the definition: JavaBean is serializable, there is a default constructor method, Member variables provide getter and setter methods. For example, the following code can be easily saved to realm,
Public class country extends realmobject { PrivateString name;Private intPopulation Public Country() { } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int getpopulation() {returnPopulation } Public void setpopulation(intPopulation) { This. Population = population; }}
You can use @PrimaryKey annotations to indicate that a member variable is a realm's primary key. For example, the following code takes a code field as a primary key,
@PrimaryKeyprivate String code;publicgetCode() { return code;}publicvoidsetCode(String code) { this.code = code;}
Create transaction
You'll find in the code below that using realm to query data is very simple, and writing data is a little tricky. Realm adheres to acid to ensure the atomicity and consistency of operations. In realm, all write operations are in one transaction.
Use beginTransaction the method to open a transaction and use the commitTransaction method to commit a transaction. The following code shows how to create and save a Country
myRealm.beginTransaction();// Create an object// Set its fieldscountry1.setName("Norway");country1.setPopulation(5165800);country1.setCode("NO");myRealm.commitTransaction();
You may have noticed that the country1 object is not Country created using the constructor method, and you want to RealmObject save this instance, you must use the createObject method to create it.
If you have to use the constructor method, don't forget to call the method to associate the object before committing the transaction copyToRealm Realm , for example:
// Create the objectnew Country();country2.setName("Russia");country2.setPopulation(146430430);country2.setCode("RU");myRealm.beginTransaction(); Country copyOfCountry2 = myRealm.copyToRealm(country2);myRealm.commitTransaction();
Inquire
Realm provides a very intuitive and straightforward API for querying operations, invoking Realm where methods, and passing the class of classes you need, and you're done creating a query, and then you can call findAll methods to iterate through all the data, and the returned data is saved in RealmResultsobject, in the following example, we query out and print out all theCountry
RealmResults<Country> results1 = myRealm.where(Country.class).findAll();for(Country c:results1) { Log.d("results1", c.getName());}// Prints Norway, RussiaRealmResults<Country> results1 = myRealm.where(Country.class).findAll();for(Country c:results1) { Log.d("results1", c.getName());}// Prints Norway, Russia
In addition, you can also use beginsWith , endsWith , lesserThan and greaterThan methods to filter the results, as shown below for example how to use greaterThan methods to query all more population than 100 million Country ,
RealmResults<Country> results2 = myRealm.where(Country.class) .greaterThan("population"100000000) .findAll();// Gets only Russia
If you want the query results to be in a certain order, you can use the findAllSorted method, which has a string parameter and a Boolean parameter, where string specifies the field to sort, and the Boolean specifies the sort method,
// Sort by name, 降序排列RealmResults<Country> results3 = myRealm.where(Country.class) .findAllSorted("name"false);// Gets Russia, Norway
To learn more about Realm the knowledge, you can refer to the official documentation
Join and use Realm on Android