Skip to content

Instantly share code, notes, and snippets.

@sagar2093
Last active March 13, 2017 17:17
Show Gist options
  • Save sagar2093/d37f6e37221e4da03cfb3f5434a810f7 to your computer and use it in GitHub Desktop.
Save sagar2093/d37f6e37221e4da03cfb3f5434a810f7 to your computer and use it in GitHub Desktop.
Realm Database version 3.0.0 example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="np.gov.mof.budget">
<!-- for internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- sd card read/write -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- for alarm manager -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- android:name="AppController" --> <!-- This is added to initialize the realm database during app initialization. -->
<application
android:name="AppController"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity
android:name=".activity.SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.Launch">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
<!-- other activity in the app.... -->
</application>
</manifest>
public class AppController extends Application{
public static final String TAG = AppController.class.getSimpleName();
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
// Initialize Realm
Realm.init(this);
//migration process here, since we donot worry about data loss database is destroyed if migration is needed.
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
}
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "np.gov.mof.budget"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def SUPPORT = '25.1.1'
compile project(':helperlib')
//compile files('libs/YouTubeAndroidPlayerApi.jar')
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
//google
compile "com.android.support:appcompat-v7:$SUPPORT"
compile "com.android.support:design:$SUPPORT"
compile "com.android.support:support-v4:$SUPPORT"
compile "com.android.support:cardview-v7:$SUPPORT"
compile "com.android.support:recyclerview-v7:$SUPPORT"
compile 'com.google.code.gson:gson:2.6.2'
//butternknife library
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
//expandable recyclerveiw
compile 'com.bignerdranch.android:expandablerecyclerview:2.1.1'
//square
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//ion for network
compile 'com.koushikdutta.ion:ion:2.1.9'
//for charts
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
//realm database
classpath "io.realm:realm-gradle-plugin:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Realm realm = Realm.getDefaultInstance();
List<User> userArrayList = new ArrayList<>();
//creating object which is not managed by realm
User user = new User();
user.setName("Kishor");
user.setAddress("Sanepa");
User anotherUser = new User();
anotherUser.setName("Sagar");
anotherUser.setAddress("Satdobato");
userArrayList.add(user);
userArrayList.add(anotherUser)
//inserting the object to realm database
realm.beginTransaction;
user = realm.copyToRealm(user); //user is now managed object which can only be updated inside transaction only
user.setAddress("Lalitpur");
realm.commitTransaction;
realm.beginTransaction;
realm.copyToRealm(anotherUser); //anotherUser is inserted
realm.copyToRealm(userArrayList);//list is inserted to database
realm.commitTransaction;
//reading objects from realm, no need of beginTransaction while reading from realm
RealmResults<User> userList = realm.where(User.class).findAll;
RealmResults<User> usersAtLalipur = realm.where(User.class).equalTo("address","Lalitpur").findAll;
User sagar = realm.where(User.class).equalTo("name","Sagar").findFirst();
}
}
public class User extends RealmObject{
private String name;
private String address;
//getter and setter or constructor or custom methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment