Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Test
- public void breaking_changes() throws InterruptedException {
- // # Global Realm listeners -- Both Realm & DynamicRealm#
- // ## The global Realm change listener will be called immediately when commitTransaction() on the same thread ##
- realm.addChangeListener(new RealmChangeListener<Realm>() {
- @Override
- public void onChange(Realm element) {
- // Notice this will be called 3rd in the next event loop in the old implementaion.
- RealmLog.info("This will be printed 2nd.");
- }
- });
- realm.executeTransaction(new Realm.Transaction() {
- @Override
- public void execute(Realm realm) {
- RealmLog.info("This will be printed 1st.");
- }
- });
- RealmLog.info("This will be printed 3rd."); // Notice in the old version, this will be called 2nd.
- // ## The global Realm change listener may be called immediately when beginTransaction() on the same thread ##
- final CountDownLatch bgCommitDone = new CountDownLatch(1);
- realm.executeTransactionAsync(new Realm.Transaction() {
- @Override
- public void execute(Realm realm) {
- bgCommitDone.countDown();
- }
- });
- bgCommitDone.await();
- realm.addChangeListener(new RealmChangeListener<Realm>() {
- @Override
- public void onChange(Realm element) {
- // Notice this will be called only once in the next event loop in the old implementation.
- RealmLog.info("This will be printed 2 times.");
- }
- });
- // beginTransaction will advance read the Realm to the latest version, and if it is different from current
- // version, the change listener will be called.
- realm.beginTransaction();
- // Notice in the old version, this will be called 2nd.
- RealmLog.info("This will be printed after first time listener called.");
- realm.commitTransaction();
- RealmLog.info("This will be printed at last."); // Notice in the old version, this will be called 2nd.
- // ## The global listener won't be called if it is added later after commitTransaction on the same thread. ##
- realm.beginTransaction();
- realm.commitTransaction();
- realm.addChangeListener(new RealmChangeListener<Realm>() {
- @Override
- public void onChange(Realm element) {
- // Notice this will be called in the next event loop in the old implenmentation.
- RealmLog.info("This won't be called.");
- }
- });
- // # Listeners on RealmObject and DynamicRealmObject #
- // ## All the changes for the global change listeners could be applied to the RealmObject listeners. ##
- // ## Calling load on findFirstAsync()'s, and if it cannot find any results, the async query won't be rerun. ##
- AllTypes allTypes = realm.where(AllTypes.class).findFirst();
- assertTrue(allTypes.isValid() == false); // It cannot find any object match the query.
- // In the old implementation, if calling load() and it cannot find any object, the query will rerun and return
- // the results until it can find an object match the query. But it is a wrong behavior. Think about
- // following code.
- AllTypes allTypes1 = realm.where(AllTypes.class).findFirst();
- assertTrue(allTypes1.isValid() == true); // It finds an object match the query.
- realm.beginTransaction();
- // In the old implementation, if the object gets deleted here and there is a pending COMPLETE_ASYNC_OBJECT event
- // in the queue, the allTypes1 will become a different object which is a wrong behavior.
- allTypes1.deleteFromRealm();
- realm.commitTransaction();
- // # RealmResults.distinct() will create a new RealmResults
- RealmResults<AllTypes> results1 = realm.where(AllTypes.class).findAll();
- RealmResults<AllTypes> results2 = results1.distinct(AllTypes.FIELD_DOUBLE);
- // For the old implementation, the results2 will be the same with results1. But we have a bug tracking it, so
- // this would be more like a bug fix than breaking change.
- assertTrue(results1 != results2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement