Skip to content

Instantly share code, notes, and snippets.

@mrcampbell
Created March 7, 2017 00:09
Show Gist options
  • Save mrcampbell/4b4b8b95de86c9cbebe00085b609d3b1 to your computer and use it in GitHub Desktop.
Save mrcampbell/4b4b8b95de86c9cbebe00085b609d3b1 to your computer and use it in GitHub Desktop.
Basic Implementation of Realm for Android
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.realm.Realm;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {
// Class specific variables and constants
public static String TAG = "MainActivity";
// Helper Variables
private Realm mRealm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set Helper Variables
Realm.init(this);
mRealm = Realm.getDefaultInstance();
// Should not be done in onCreate, but this is just a demonstration
performRealmTransactions(mRealm);
}
public void performRealmTransactions(Realm realm) {
Log.d(TAG, "Creating Mike Campbell");
realmCreate(realm, new Person("Mike", "Campbell"));
Log.d(TAG, "Creating Jim Smith");
realmCreate(realm, new Person("Jim", "Smith"));
Log.d(TAG, "Updating Mike Campbell to Michael Campbell");
realmUpdatePerson(realm, "Mike", "Michael");
Log.d(TAG, "Displaying all Persons");
realmLogAllPersons(realm);
Log.d(TAG, "Deleting all Jim's");
realmDeleteAllPersonsByFirstName(realm, "Jim");
Log.d(TAG, "Displaying all Persons");
realmLogAllPersons(realm);
Log.d(TAG, "Deleting all Persons");
realmDeleteAllPersons(realm);
Log.d(TAG, "Displaying all Persons");
realmLogAllPersons(realm);
}
public void realmLogAllPersons(Realm realm) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Person> results = realm.where(Person.class).findAll();
for (Person p : results) {
Log.d(TAG + ":FindAll", p.toString());
}
}
});
}
public void realmCreate(Realm realm, final Person p) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Person realmPerson = realm.createObject(Person.class);
realmPerson.setFirstName(p.getFirstName());
realmPerson.setLastName(p.getLastName());
}
});
}
public Person realmFindPersonByFirstName(Realm realm, String firstName) {
final Person person = realm.where(Person.class)
.equalTo("firstName", firstName)
.findFirst();
Log.d(TAG + ":Find", person.toString());
return person;
}
public void realmUpdatePerson(Realm realm, String currentFirstName, final String newFirstName) {
final Person person = realmFindPersonByFirstName(realm, currentFirstName);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
person.setFirstName(newFirstName);
}
});
Log.d(TAG, "Updated Person: " + currentFirstName + " -> " + newFirstName);
Log.d(TAG, realmFindPersonByFirstName(realm, "Michael").toString());
}
public void realmDeleteAllPersons(Realm realm) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.delete(Person.class);
}
});
}
public void realmDeleteAllPersonsByFirstName(Realm realm, final String firstName) {
final RealmResults<Person> result = realm.where(Person.class)
.equalTo("firstName", firstName)
.findAll();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
result.deleteAllFromRealm();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "Activity destroyed.");
mRealm.close();
}
}
package com.plugtech.realmtoolbox;
import io.realm.RealmObject;
/** PERSON CLASS. FOR DEMONSTRATIVE PURPOSES ONLY. **/
public class Person extends RealmObject {
private String firstName;
private String lastName;
public Person() { /* required default constructor */}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person: " + getFirstName() + " " + getLastName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment