{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/41003670","id":41003670,"diff_hunk":"@@ -586,14 +626,278 @@ public static synchronized void migrateRealm(RealmConfiguration configuration, R\n     private class RealmCallback implements Handler.Callback {\n         @Override\n         public boolean handleMessage(Message message) {\n-            if (message.what == REALM_CHANGED) {\n-                sharedGroupManager.advanceRead();\n-                sendNotifications();\n+            switch (message.what) {\n+                case REALM_CHANGED: {\n+                    if (threadContainsAsyncQueries()) {\n+                        if (updateAsyncQueriesTask != null && !updateAsyncQueriesTask.isDone()) {\n+                            // try to cancel any pending update since we're submitting a new one anyway\n+                            updateAsyncQueriesTask.cancel(true);\n+                            asyncQueryExecutor.getQueue().remove(updateAsyncQueriesTask);\n+                            RealmLog.d(\"REALM_CHANGED realm:\"+ BaseRealm.this + \" cancelling pending REALM_UPDATE_ASYNC_QUERIES updates\");\n+                        }\n+                        RealmLog.d(\"REALM_CHANGED realm:\"+ BaseRealm.this + \" updating async queries, total: \" + asyncRealmResults.size());\n+                        // prepare a QueryUpdateTask to current async queries in this thread\n+                        QueryUpdateTask.Builder.UpdateQueryStep updateQueryStep = QueryUpdateTask.newBuilder()\n+                                .realmConfiguration(getConfiguration());\n+                        QueryUpdateTask.Builder.RealmResultsQueryStep realmResultsQueryStep = null;\n+\n+                        // we iterate over non GC'd async RealmResults then add them to the list to be updated (in a batch)\n+                        Iterator<Map.Entry<WeakReference<RealmResults<?>>, RealmQuery<?>>> iterator = asyncRealmResults.entrySet().iterator();\n+                        while (iterator.hasNext()) {\n+                            Map.Entry<WeakReference<RealmResults<?>>, RealmQuery<?>> entry = iterator.next();\n+                            WeakReference<RealmResults<?>> weakReference = entry.getKey();\n+                            RealmResults<?> realmResults = weakReference.get();\n+                            if (realmResults == null) {\n+                                // GC'd instance remove from the list\n+                                iterator.remove();\n+\n+                            } else {\n+                                realmResultsQueryStep = updateQueryStep.add(weakReference,\n+                                        entry.getValue().handoverQueryPointer(),\n+                                        entry.getValue().getArgument());\n+                            }\n+\n+                            // Note: we're passing an WeakRef of a RealmResults to another thread\n+                            //       this is safe as long as we don't invoke any of the RealmResults methods.\n+                            //       we're just using it as a Key in an IdentityHashMap (i.e doesn't call\n+                            //       AbstractList's hashCode, that require accessing objects from another thread)\n+                            //\n+                            //       watch out when you debug, as you're IDE try to evaluate RealmResults\n+                            //       which break the Thread confinement constraints.\n+                        }\n+                        if (realmResultsQueryStep != null) {\n+                            QueryUpdateTask queryUpdateTask = realmResultsQueryStep\n+                                    .sendToHandler(handler, REALM_UPDATE_ASYNC_QUERIES)\n+                                    .build();\n+                            updateAsyncQueriesTask = asyncQueryExecutor.submit(queryUpdateTask);\n+                        }\n+\n+                    } else {\n+                        RealmLog.d(\"REALM_CHANGED realm:\"+ BaseRealm.this + \" no async queries, advance_read\");\n+                        sharedGroupManager.advanceRead();\n+                        sendNotifications();\n+                    }\n+                    break;\n+                }\n+                case REALM_COMPLETED_ASYNC_QUERY: {\n+                    // one async query has completed","path":"realm/src/main/java/io/realm/BaseRealm.java","position":148,"original_position":148,"commit_id":"9f35f0a433ca66d4bb128aa790ae77bd048695e5","original_commit_id":"9f35f0a433ca66d4bb128aa790ae77bd048695e5","user":{"login":"cmelchior","id":406066,"avatar_url":"https://avatars.githubusercontent.com/u/406066?v=3","gravatar_id":"","url":"https://api.github.com/users/cmelchior","html_url":"https://github.com/cmelchior","followers_url":"https://api.github.com/users/cmelchior/followers","following_url":"https://api.github.com/users/cmelchior/following{/other_user}","gists_url":"https://api.github.com/users/cmelchior/gists{/gist_id}","starred_url":"https://api.github.com/users/cmelchior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmelchior/subscriptions","organizations_url":"https://api.github.com/users/cmelchior/orgs","repos_url":"https://api.github.com/users/cmelchior/repos","events_url":"https://api.github.com/users/cmelchior/events{/privacy}","received_events_url":"https://api.github.com/users/cmelchior/received_events","type":"User","site_admin":false},"body":"Move to own method","created_at":"2015-10-02T09:16:28Z","updated_at":"2015-10-02T09:16:28Z","html_url":"https://github.com/realm/realm-java/pull/1214#discussion_r41003670","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/1214","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/41003670"},"html":{"href":"https://github.com/realm/realm-java/pull/1214#discussion_r41003670"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1214"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/1214","id":37651413,"html_url":"https://github.com/realm/realm-java/pull/1214","diff_url":"https://github.com/realm/realm-java/pull/1214.diff","patch_url":"https://github.com/realm/realm-java/pull/1214.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/1214","number":1214,"state":"open","locked":false,"title":"async queries & write transaction","user":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"body":" __Support async queries & write transactions:__\r\n*new methods:*\r\n* `findAllAsync`\r\n* `findAllSortedAsync`\r\n* `findFirstAsync`\r\n\r\nthey accept the same parameters as their synchronous counterpart, but they return immediately a _promise_, as described [here] (https://github.com/realm/realm-wiki/wiki/%5BDRAFT%5D-Asynchronous-queries-design-proposal)\r\nNote: the interface `RealmCollection` was not add so far, because we have only one return type `RealmResults` for both sync & async, plus retrieving  `RealmList` asynchronously was outside of this scope\r\n\r\n__Support async write transactions:__\r\n`executeTransaction` is overloaded to accept a new parameters `Transaction.Callback`. This will run the write transaction on a worker thread.\r\nThe async transaction returns a `Request` object that can be used to cancel a scheduled/pending write transaction (if it didn't already complete)\r\n\r\nFixes #896 #1224 #1207 \r\n\r\n- [x] reflect breaking changes from Core `0.93.0` branch & build against it \r\n- [ ] rename `where` to `query` ?","created_at":"2015-06-15T00:16:28Z","updated_at":"2015-10-02T09:16:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"767d603d28a01f5d958a8750d91ab06667df4663","assignee":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/1214/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/1214/comments","review_comment_url":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/1214/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/9f35f0a433ca66d4bb128aa790ae77bd048695e5","head":{"label":"realm:nh-async-query","ref":"nh-async-query","sha":"9f35f0a433ca66d4bb128aa790ae77bd048695e5","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","created_at":"2012-04-20T09:18:42Z","updated_at":"2015-10-02T06:38:51Z","pushed_at":"2015-10-02T08:34:34Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":57707,"stargazers_count":2549,"watchers_count":2549,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":259,"mirror_url":null,"open_issues_count":153,"forks":259,"open_issues":153,"watchers":2549,"default_branch":"master"}},"base":{"label":"realm:master","ref":"master","sha":"9dc76c269f71fd60d2c6ae405aad905758214b52","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","created_at":"2012-04-20T09:18:42Z","updated_at":"2015-10-02T06:38:51Z","pushed_at":"2015-10-02T08:34:34Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":57707,"stargazers_count":2549,"watchers_count":2549,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":259,"mirror_url":null,"open_issues_count":153,"forks":259,"open_issues":153,"watchers":2549,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1214"},"html":{"href":"https://github.com/realm/realm-java/pull/1214"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/1214"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/1214/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1214/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1214/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/9f35f0a433ca66d4bb128aa790ae77bd048695e5"}}}}