Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async queries don't update when using link #1717

Closed
nhachicha opened this issue Nov 2, 2015 · 0 comments
Closed

Async queries don't update when using link #1717

nhachicha opened this issue Nov 2, 2015 · 0 comments
Assignees
Labels
Blocked This issue is blocked by another issue T-Bug

Comments

@nhachicha
Copy link
Collaborator

as mentioned here updating an async query containing a link will not bring latest result.
UT to reproduce

public void testQueryingLink () throws Throwable {
        final CountDownLatch signalCallbackFinished = new CountDownLatch(1);
        final CountDownLatch signalClosedRealm = new CountDownLatch(1);
        final AtomicInteger numberOfInvocations = new AtomicInteger(0);
        final Realm[] realm = new Realm[1];
        final Throwable[] threadAssertionError = new Throwable[1];
        final Looper[] backgroundLooper = new Looper[1];
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                backgroundLooper[0] = Looper.myLooper();

                try {
                    realm[0] = openRealmInstance("testQueryingLink");


                    final RealmResults<Dog> allAsync = realm[0].where(Dog.class).equalTo("owner.name", "kiba").findAllAsync();
                    allAsync.addChangeListener(new RealmChangeListener() {
                        @Override
                        public void onChange() {
                            switch (numberOfInvocations.incrementAndGet()) {
                                case 1: {
                                    assertEquals(0, allAsync.size());
                                    assertTrue(allAsync.isLoaded());
                                    assertTrue(allAsync.isValid());
                                    assertTrue(allAsync.isEmpty());
                                    final CountDownLatch wait = new CountDownLatch(1);
                                    final RealmConfiguration configuration = realm[0].getConfiguration();
                                    new AsyncTask() {
                                        @Override
                                        protected Object doInBackground(Object[] objects) {
                                            Realm instance = Realm.getInstance(configuration);
                                            instance.beginTransaction();
                                            Dog dog = instance.createObject(Dog.class);
                                            dog.setAge(10);
                                            dog.setName("Akamaru");
                                            Owner kiba = instance.createObject(Owner.class);
                                            kiba.setName("kiba");
                                            dog.setOwner(kiba);
                                            instance.commitTransaction();
                                            wait.countDown();
                                            return null;
                                        }
                                    }.execute();

                                    try {
                                        wait.await();
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    break;
                                }
                                case 2: {
                                    assertEquals(1, realm[0].allObjects(Dog.class).size());
                                    assertEquals(1, realm[0].allObjects(Owner.class).size());
                                    assertEquals(1, allAsync.size());
                                    assertTrue(allAsync.isLoaded());
                                    assertTrue(allAsync.isValid());
                                    assertTrue(allAsync.isEmpty());
                                    signalCallbackFinished.countDown();
                                    break;
                                }
                                default:
                                    throw new IllegalStateException("invalid number of invocation");
                            }
                        }
                    });

                    Looper.loop();

                } catch (Throwable e) {
                    e.printStackTrace();
                    threadAssertionError[0] = e;

                } finally {
                    if (signalCallbackFinished.getCount() > 0) {
                        signalCallbackFinished.countDown();
                    }
                    if (realm.length > 0 && realm[0] != null) {
                        realm[0].close();
                    }
                    signalClosedRealm.countDown();
                }
            }
        });

        exitOrThrow(executorService, signalCallbackFinished, signalClosedRealm, backgroundLooper, threadAssertionError);
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Blocked This issue is blocked by another issue T-Bug
Projects
None yet
Development

No branches or pull requests

1 participant