10 steps to get started with Realm for Android.
This article is written for someone who just wants to give it a try anyway.
If you're interested in details, please read the official docs.
The application that you're going to make from now, inserts 1 user with 2 breads every time you start the app. :)
1. Add this line to the project level build.gradle.
classpath 'io.realm:realm-gradle-plugin:3.1.4'
2. Add this line to the top of the application level build.gradle.
apply plugin: 'realm-android'
3. Refresh gradle dependencies.
4. Configure a Realm in MyApplication.java.
5. Add MyApplication to application tag in the app's AndroidManifest.xml.
<application android:name=".MyApplication"> </application>
6. Create a User class.
You can mark a field as a primary key with @PrimaryKey.
Other annotation types summary is HERE.
7. Create a Bread class.
8. Write insert function in MainActivity.java.
As I mentioned before, this app inserts 1 user and 2 breads into Realm DB when onCreate() is called.
There is an auto-incrementing ID helper but it's not recommended when you:
1) Create Realm Objects in multiple processes. 2) Want to share the Realm between multiple devices at some point in the future.
Here is an example of auto-Increment ID for creating objects across processes.
Example of delete function.
User u = mRealm.where(User.class).equalTo("id", id).findFirst(); u.deleteFromRealm();
9. Copy database to somewhere accessible.
Realm provides a Mac app to edit and read your database.
Currently, this app doesn't support accessing database directly.
You have to copy it from emulator or device to somewhere accessible.
$ adb shell "run-as your.package.name chmod 666 /data/data/your.package.name/files/default.realm” $ adb exec-out run-as your.package.name cat files/default.realm > default.realm $ adb shell "run-as your.package.name chmod 600 /data/data/your.package.name/files/default.realm"
10. View your database in Realm Browser.
Choose 'Open Realm File' and you can view it.
User table
Bread table
You're done!
Now you're able to explore Realm world by yourself. Enjoy! :)