Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***********************ALL SNIPPETS ARE PASTED ONE BELOW EACH OTHER HERE ******************************
- ***********************SNIPPET 1***********************************************************************
- public class Drop extends RealmObject {
- private String what;
- @PrimaryKey
- private long added;
- private long when;
- private boolean completed;
- public Drop() {
- }
- public String getWhat() {
- return what;
- }
- public void setWhat(String what) {
- this.what = what;
- }
- public long getAdded() {
- return added;
- }
- public void setAdded(long added) {
- this.added = added;
- }
- public long getWhen() {
- return when;
- }
- public void setWhen(long when) {
- this.when = when;
- }
- public boolean isCompleted() {
- return completed;
- }
- public void setCompleted(boolean completed) {
- this.completed = completed;
- }
- }
- ***********************SNIPPET 2***********************************************************************
- public class Person extends RealmObject {
- @PrimaryKey
- private String email;
- private RealmList<Drop> drops = new RealmList<>();
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public RealmList<Drop> getDrops() {
- return drops;
- }
- public void setDrops(RealmList<Drop> drops) {
- this.drops = drops;
- }
- }
- ***********************SNIPPET 3***********************************************************************
- //Get an instance of the Realm class
- Realm realm = Realm.getDefaultInstance();
- //Supply the values of the properties needed to create a Drop object
- Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
- //Start a new transaction
- realm.beginTransaction();
- //Save the data
- realm.copyToRealm(drop);
- //Commit or cancel the transaction as per your logic
- realm.commitTransaction();
- ***********************SNIPPET 4***********************************************************************
- //Get an instance of the Realm class
- Realm realm = Realm.getDefaultInstance();
- //Start a new transaction
- realm.beginTransaction();
- Drop drop = realm.createObject(Drop.class);
- //Set the required properties which will be saved directly to Realm
- drop.setWhat(what);
- drop.setAdded(now);
- drop.setWhen(mInputWhen.getTime());
- drop.setCompleted(false);
- //Commit or cancel the transaction as per your logic
- realm.commitTransaction();
- ***********************SNIPPET 5***********************************************************************
- //Get an instance of Realm
- Realm realm = Realm.getDefaultInstance();
- //Specify which objects you want to read
- RealmResults<Drop> drops = realm.where(Drop.class).findAll();
- ***********************SNIPPET 6***********************************************************************
- //Get an instance of Realm
- Realm realm = Realm.getDefaultInstance();
- //Find the first person whose email is ‘abc@gmail.com’
- Person person = realm.where(Person.class).equalTo("email","abc@gmail.com").findFirst();
- //Use the person object retrieved above to find all drops
- RealmList<Drop> drops = person.getDrops();
- ***********************SNIPPET 7***********************************************************************
- Realm realm = Realm.getDefaultInstance();
- //Find the person whose email is ‘abc@gmail.com’
- Person person= realm.where(Person.class).equalTo("email","abc@gmail.com").findFirst();
- realm.beginTransaction();
- //Use the setter method of the Person class to change the email.
- person.setEmail("def@gmail.com");
- realm.commitTransaction();
- ***********************SNIPPET 8***********************************************************************
- Delete all Drops related to ‘Party’
- Realm realm = Realm.getDefaultInstance();
- //Find all drops with ‘what = Party’
- RealmResults<Drop> results=realm.where(Drop.class).equalTo("what","Party").findAll();
- realm.beginTransaction();
- results.clear();
- realm.commitTransaction();
- Delete the Person whose email is ‘xyz@gmail.com’
- Realm realm = Realm.getDefaultInstance();
- //Find the person whose ‘email = xyz@gmail.com’
- Person person = realm.where(Person.class).equalTo("email", "xyz@gmail.com").findFirst();
- realm.beginTransaction();
- person.removeFromRealm();
- realm.commitTransaction();
- ***********************SNIPPET 9***********************************************************************
- Realm realm = Realm.getDefaultInstance();
- //Execute a transaction
- realm.executeTransaction(new Realm.Transaction() {
- @Override
- public void execute(Realm realm) {
- Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
- //Save the drop to Realm
- realm.copyToRealm(drop);
- }
- });
- ***********************SNIPPET 10**********************************************************************
- Realm realm = Realm.getDefaultInstance();
- realm.executeTransaction(new Realm.Transaction() {
- @Override
- public void execute(Realm realm) {
- Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
- realm.copyToRealm(drop);
- }
- }, new Realm.Transaction.Callback() {
- @Override
- public void onSuccess() {
- Log.d(TAG, "onSuccess: ");
- }
- @Override
- public void onError(Exception e) {
- Log.d(TAG, "onError: ");
- }
- });
- ***********************SNIPPET 11**********************************************************************
- Find all the people whose email is either ‘gmail’ or ‘hotmail’
- Realm realm = Realm.getDefaultInstance();
- //Notice how contains is used in conjunction with or
- RealmResults<Person> results = realm.where(Person.class).contains("email","gmail").or().contains("email", "hotmail").findAll();
- for(Person person: results){
- Log.d(TAG, "addAction: "+person.getEmail());
- }
- ***********************SNIPPET 12**********************************************************************
- //Sort all results on the basis of email values alphabetically
- results.sort("email");
- //Sort all results on the basis of email values alphabetically in the //descending order
- results.sort("email", Sort.DESCENDING);
- ***********************SNIPPET 13**********************************************************************
- RealmResults<Person> results = realm.where(Person.class).findAll();
- int max = results.max("age").intValue();
- int min = results.min("age").intValue();
- double average = results.average("age");
- long sum = results.sum("age").longValue();
- ***********************SNIPPET 14**********************************************************************
- Using a for-each loop
- RealmResults<Person> results = realm.where(Person.class).findAll();
- for(Person person : results)
- {
- //Read the values and process them
- }
- Using a for loop
- RealmResults<Person> results = realm.where(Person.class).findAll();
- for (int i = 0; i < results.size(); i++) {
- Person person = results.get(i);
- //Do something here with the Person
- }
- ***********************SNIPPET 15**********************************************************************
- RealmResults<Drop> results = realm.where(Drop.class).findAllAsync();
- ***********************SNIPPET 16**********************************************************************
- private RealmChangeListener mChangeListener = new RealmChangeListener() {
- @Override
- public void onChange() {
- //Use the result here to do what you want
- }
- };
- ***********************SNIPPET 17**********************************************************************
- @Override
- protected void onStart() {
- super.onStart();
- mResults.addChangeListener(mChangeListener);
- }
- @Override
- protected void onStop() {
- super.onStop();
- mResults.removeChangeListener(mChangeListener);
- }
Add Comment
Please, Sign In to add comment