{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/1224","labels_url":"https://api.github.com/repos/realm/realm-java/issues/1224/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/1224/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/1224/events","html_url":"https://github.com/realm/realm-java/issues/1224","id":89536500,"number":1224,"title":"Refactor RealmQuery to allow async queries","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},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/Feature","name":"Feature","color":"c7def8"},{"url":"https://api.github.com/repos/realm/realm-java/labels/P1","name":"P1","color":"ededed"}],"state":"open","locked":false,"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,"comments":16,"created_at":"2015-06-19T11:09:02Z","updated_at":"2015-06-23T08:21:58Z","closed_at":null,"body":"Now that we have a POC of a working async queries with a convergence strategy https://github.com/realm/realm-java/pull/1214 \r\nI suggest the following refactor to enable `RealmQuery` to be used seamlessly for both sync & async queries.\r\n\r\nUnless we define a DSL that represent a query we can push (in one call) from Java to Core. We need to evaluate lazily the query once we call `findAll`, `findFirst` etc. Instead of calling _Core_ for each step (e.g. `greaterThan`, `isNotNull`). \r\nAs suggested by @cmelchior  we should approach it as a **command pattern**, basically queue all calls to native in **a list of `Runnable`**, then call `execute` to perform each call. at this point we can overload `execute` to run on a background thread (with a `callback`)\r\n\r\nthe result will be like this\r\n```java\r\n// Build the query looking at all users:\r\nRealmQuery<User> query = realm.where(User.class);\r\n\r\n// Add query conditions:\r\nquery.equalTo(\"name\", \"John\");\r\nquery.or().equalTo(\"name\", \"Peter\");\r\n\r\n// Execute the query in the caller thread\r\nRealmResults<User> result1 = query.findAll();\r\n\r\n// Execute the query on a worker thread:\r\nRealmQuery.AsyncRequest result1 = query.findAllAsync(new RealmQuery.Callback() {\r\n  public void onSuccess(RealmResults<Foo> results) {\r\n    // Use results\r\n }\r\n});\r\n\r\nrealm.where(Foo.class).findFirstAsync(new RealmQuery.Callback() {\r\n  public void onSuccess(Foo result) {\r\n    // Use result\r\n }\r\n});\r\n\r\n// User can cancel the pending query\r\nresult1.cancel();\r\n```\r\nWDYT? @bmunkholm @emanuelez @kneth \r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/114403784","html_url":"https://github.com/realm/realm-java/issues/1224#issuecomment-114403784","issue_url":"https://api.github.com/repos/realm/realm-java/issues/1224","id":114403784,"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},"created_at":"2015-06-23T08:21:58Z","updated_at":"2015-06-23T08:21:58Z","body":"Yes, expect that would introduce a dependency on RxJava, which not all would want to drag into their project as it is both complicated and quite heavyweight. \r\n\r\nWe have an issue for explicit RxJava support ( #865 ), where something like returning an observable instead would be a perfect fit. This would probably be an add-on just like RxAndroid is to RxJava, but we haven't decided fully on that yet."}}