vivz

Realm article devoxx

Mar 18th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.75 KB | None | 0 0
  1. ***********************ALL SNIPPETS ARE PASTED ONE BELOW EACH OTHER HERE ******************************
  2. ***********************SNIPPET 1***********************************************************************
  3. public class Drop extends RealmObject {
  4.  
  5.     private String what;
  6.     @PrimaryKey
  7.     private long added;
  8.     private long when;
  9.     private boolean completed;
  10.  
  11.     public Drop() {
  12.     }
  13.  
  14.     public String getWhat() {
  15.         return what;
  16.     }
  17.  
  18.     public void setWhat(String what) {
  19.         this.what = what;
  20.     }
  21.  
  22.     public long getAdded() {
  23.         return added;
  24.     }
  25.  
  26.     public void setAdded(long added) {
  27.         this.added = added;
  28.     }
  29.  
  30.     public long getWhen() {
  31.         return when;
  32.     }
  33.  
  34.     public void setWhen(long when) {
  35.         this.when = when;
  36.     }
  37.  
  38.     public boolean isCompleted() {
  39.         return completed;
  40.     }
  41.  
  42.     public void setCompleted(boolean completed) {
  43.         this.completed = completed;
  44.     }
  45. }
  46. ***********************SNIPPET 2***********************************************************************
  47. public class Person extends RealmObject {
  48.     @PrimaryKey
  49.     private String email;
  50.     private RealmList<Drop> drops = new RealmList<>();
  51.  
  52.     public String getEmail() {
  53.         return email;
  54.     }
  55.  
  56.     public void setEmail(String email) {
  57.         this.email = email;
  58.     }
  59.  
  60.     public RealmList<Drop> getDrops() {
  61.         return drops;
  62.     }
  63.  
  64.     public void setDrops(RealmList<Drop> drops) {
  65.         this.drops = drops;
  66.     }
  67. }
  68. ***********************SNIPPET 3***********************************************************************
  69. //Get an instance of the Realm class
  70. Realm realm = Realm.getDefaultInstance();
  71. //Supply the values of the properties needed to create a Drop object
  72. Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
  73. //Start a new transaction
  74. realm.beginTransaction();
  75. //Save the data
  76. realm.copyToRealm(drop);
  77. //Commit or cancel the transaction as per your logic
  78. realm.commitTransaction();
  79. ***********************SNIPPET 4***********************************************************************
  80. //Get an instance of the Realm class
  81. Realm realm = Realm.getDefaultInstance();
  82. //Start a new transaction
  83. realm.beginTransaction();
  84. Drop drop = realm.createObject(Drop.class);
  85. //Set the required properties which will be saved directly to Realm
  86. drop.setWhat(what);
  87. drop.setAdded(now);
  88. drop.setWhen(mInputWhen.getTime());
  89. drop.setCompleted(false);
  90. //Commit or cancel the transaction as per your logic
  91. realm.commitTransaction();
  92. ***********************SNIPPET 5***********************************************************************
  93. //Get an instance of Realm
  94. Realm realm = Realm.getDefaultInstance();
  95. //Specify which objects you want to read
  96. RealmResults<Drop> drops = realm.where(Drop.class).findAll();
  97. ***********************SNIPPET 6***********************************************************************
  98. //Get an instance of Realm
  99. Realm realm = Realm.getDefaultInstance();
  100. //Find the first person whose email is ‘abc@gmail.com’
  101. Person person = realm.where(Person.class).equalTo("email","abc@gmail.com").findFirst();
  102. //Use the person object retrieved above to find all drops
  103. RealmList<Drop> drops = person.getDrops();
  104. ***********************SNIPPET 7***********************************************************************
  105. Realm realm = Realm.getDefaultInstance();
  106. //Find the person whose email is ‘abc@gmail.com’
  107. Person person= realm.where(Person.class).equalTo("email","abc@gmail.com").findFirst();
  108. realm.beginTransaction();
  109. //Use the setter method of the Person class to change the email.
  110. person.setEmail("def@gmail.com");
  111. realm.commitTransaction();
  112. ***********************SNIPPET 8***********************************************************************
  113. Delete all Drops related to ‘Party’
  114. Realm realm = Realm.getDefaultInstance();
  115. //Find all drops with ‘what = Party’
  116. RealmResults<Drop> results=realm.where(Drop.class).equalTo("what","Party").findAll();
  117. realm.beginTransaction();
  118. results.clear();
  119. realm.commitTransaction();
  120.  
  121. Delete the Person whose email is ‘xyz@gmail.com
  122. Realm realm = Realm.getDefaultInstance();
  123. //Find the person whose ‘email = xyz@gmail.com’
  124. Person person = realm.where(Person.class).equalTo("email", "xyz@gmail.com").findFirst();
  125. realm.beginTransaction();
  126. person.removeFromRealm();
  127. realm.commitTransaction();
  128. ***********************SNIPPET 9***********************************************************************
  129. Realm realm = Realm.getDefaultInstance();
  130. //Execute a transaction
  131. realm.executeTransaction(new Realm.Transaction() {
  132.     @Override
  133.     public void execute(Realm realm) {
  134.         Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
  135.       //Save the drop to Realm
  136.         realm.copyToRealm(drop);
  137.     }
  138. });
  139. ***********************SNIPPET 10**********************************************************************
  140. Realm realm = Realm.getDefaultInstance();
  141. realm.executeTransaction(new Realm.Transaction() {
  142.     @Override
  143.     public void execute(Realm realm) {
  144.         Drop drop = new Drop(what, now, mInputWhen.getTime(), false);
  145.         realm.copyToRealm(drop);
  146.     }
  147. }, new Realm.Transaction.Callback() {
  148.     @Override
  149.     public void onSuccess() {
  150.         Log.d(TAG, "onSuccess: ");
  151.     }
  152.  
  153.     @Override
  154.     public void onError(Exception e) {
  155.         Log.d(TAG, "onError: ");
  156.     }
  157. });
  158. ***********************SNIPPET 11**********************************************************************
  159. Find all the people whose email is either ‘gmail’ or ‘hotmail’
  160. Realm realm = Realm.getDefaultInstance();
  161. //Notice how contains is used in conjunction with or
  162. RealmResults<Person> results = realm.where(Person.class).contains("email","gmail").or().contains("email", "hotmail").findAll();
  163. for(Person person: results){
  164.     Log.d(TAG, "addAction: "+person.getEmail());
  165. }
  166. ***********************SNIPPET 12**********************************************************************
  167. //Sort all results on the basis of email values alphabetically
  168. results.sort("email");
  169. //Sort all results on the basis of email values alphabetically in the //descending order
  170. results.sort("email", Sort.DESCENDING);
  171.  
  172. ***********************SNIPPET 13**********************************************************************
  173.  
  174. RealmResults<Person> results = realm.where(Person.class).findAll();
  175. int max = results.max("age").intValue();
  176. int min = results.min("age").intValue();
  177. double average = results.average("age");
  178. long sum = results.sum("age").longValue();
  179.  
  180. ***********************SNIPPET 14**********************************************************************
  181. Using a for-each loop
  182. RealmResults<Person> results = realm.where(Person.class).findAll();
  183. for(Person person : results)
  184. {
  185.     //Read the values and process them
  186. }
  187.  
  188. Using a for loop
  189. RealmResults<Person> results = realm.where(Person.class).findAll();
  190. for (int i = 0; i < results.size(); i++) {
  191.     Person person = results.get(i);
  192.     //Do something here with the Person
  193. }
  194.  
  195. ***********************SNIPPET 15**********************************************************************
  196. RealmResults<Drop> results = realm.where(Drop.class).findAllAsync();
  197. ***********************SNIPPET 16**********************************************************************
  198. private RealmChangeListener mChangeListener = new RealmChangeListener() {
  199.     @Override
  200.     public void onChange() {
  201.         //Use the result here to do what you want      
  202.     }
  203. };
  204.  
  205. ***********************SNIPPET 17**********************************************************************
  206. @Override
  207. protected void onStart() {
  208.     super.onStart();
  209.     mResults.addChangeListener(mChangeListener);
  210. }
  211.  
  212. @Override
  213. protected void onStop() {
  214.     super.onStop();
  215.     mResults.removeChangeListener(mChangeListener);
  216. }
Add Comment
Please, Sign In to add comment