Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. @Test
  2. public void breaking_changes() throws InterruptedException {
  3. // # Global Realm listeners -- Both Realm & DynamicRealm#
  4. // ## The global Realm change listener will be called immediately when commitTransaction() on the same thread ##
  5. realm.addChangeListener(new RealmChangeListener<Realm>() {
  6. @Override
  7. public void onChange(Realm element) {
  8. // Notice this will be called 3rd in the next event loop in the old implementaion.
  9. RealmLog.info("This will be printed 2nd.");
  10. }
  11. });
  12.  
  13. realm.executeTransaction(new Realm.Transaction() {
  14. @Override
  15. public void execute(Realm realm) {
  16. RealmLog.info("This will be printed 1st.");
  17. }
  18. });
  19.  
  20. RealmLog.info("This will be printed 3rd."); // Notice in the old version, this will be called 2nd.
  21.  
  22. // ## The global Realm change listener may be called immediately when beginTransaction() on the same thread ##
  23. final CountDownLatch bgCommitDone = new CountDownLatch(1);
  24. realm.executeTransactionAsync(new Realm.Transaction() {
  25. @Override
  26. public void execute(Realm realm) {
  27. bgCommitDone.countDown();
  28. }
  29. });
  30.  
  31. bgCommitDone.await();
  32. realm.addChangeListener(new RealmChangeListener<Realm>() {
  33. @Override
  34. public void onChange(Realm element) {
  35. // Notice this will be called only once in the next event loop in the old implementation.
  36. RealmLog.info("This will be printed 2 times.");
  37. }
  38. });
  39.  
  40. // beginTransaction will advance read the Realm to the latest version, and if it is different from current
  41. // version, the change listener will be called.
  42. realm.beginTransaction();
  43. // Notice in the old version, this will be called 2nd.
  44. RealmLog.info("This will be printed after first time listener called.");
  45. realm.commitTransaction();
  46.  
  47. RealmLog.info("This will be printed at last."); // Notice in the old version, this will be called 2nd.
  48.  
  49. // ## The global listener won't be called if it is added later after commitTransaction on the same thread. ##
  50. realm.beginTransaction();
  51. realm.commitTransaction();
  52.  
  53. realm.addChangeListener(new RealmChangeListener<Realm>() {
  54. @Override
  55. public void onChange(Realm element) {
  56. // Notice this will be called in the next event loop in the old implenmentation.
  57. RealmLog.info("This won't be called.");
  58. }
  59. });
  60.  
  61. // # Listeners on RealmObject and DynamicRealmObject #
  62. // ## All the changes for the global change listeners could be applied to the RealmObject listeners. ##
  63. // ## Calling load on findFirstAsync()'s, and if it cannot find any results, the async query won't be rerun. ##
  64. AllTypes allTypes = realm.where(AllTypes.class).findFirst();
  65. assertTrue(allTypes.isValid() == false); // It cannot find any object match the query.
  66. // In the old implementation, if calling load() and it cannot find any object, the query will rerun and return
  67. // the results until it can find an object match the query. But it is a wrong behavior. Think about
  68. // following code.
  69. AllTypes allTypes1 = realm.where(AllTypes.class).findFirst();
  70. assertTrue(allTypes1.isValid() == true); // It finds an object match the query.
  71. realm.beginTransaction();
  72. // In the old implementation, if the object gets deleted here and there is a pending COMPLETE_ASYNC_OBJECT event
  73. // in the queue, the allTypes1 will become a different object which is a wrong behavior.
  74. allTypes1.deleteFromRealm();
  75. realm.commitTransaction();
  76.  
  77. // # RealmResults.distinct() will create a new RealmResults
  78. RealmResults<AllTypes> results1 = realm.where(AllTypes.class).findAll();
  79. RealmResults<AllTypes> results2 = results1.distinct(AllTypes.FIELD_DOUBLE);
  80. // For the old implementation, the results2 will be the same with results1. But we have a bug tracking it, so
  81. // this would be more like a bug fix than breaking change.
  82. assertTrue(results1 != results2);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement