{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/47820375","id":47820375,"diff_hunk":"@@ -0,0 +1,221 @@\n+/*\n+ * Copyright 2015 Realm Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package io.realm.examples.rxjava.gotchas;\n+\n+import android.app.Activity;\n+import android.os.Bundle;\n+import android.view.ViewGroup;\n+import android.widget.TextView;\n+\n+import java.util.List;\n+import java.util.Random;\n+\n+import io.realm.Realm;\n+import io.realm.Sort;\n+import io.realm.examples.rxjava.R;\n+import io.realm.examples.rxjava.model.Person;\n+import rx.Observable;\n+import rx.Subscription;\n+import rx.functions.Action1;\n+import rx.functions.Func1;\n+import rx.schedulers.Schedulers;\n+import rx.subscriptions.CompositeSubscription;\n+\n+/**\n+ * This class shows some of the current obstacles when combining RxJava and Realm. 2 things are\n+ * important to keep in mind.\n+ *\n+ * 1) Thread confinement: Realm objects are thread confined, so trying to access them from another thread will throw\n+ *    an exception.\n+ *\n+ * 2) Realm objects auto-refresh. This means that the same object will alter it's state automatically over time.\n+ *\n+ * Both of these characteristics doesn't play well with RxJava's threading model which favor immutable thread-safe\n+ * objects.\n+ *\n+ * Work is in progress to make it easier to work around these constraints. See\n+ * - https://github.com/realm/realm-java/issues/1208\n+ * - https://github.com/realm/realm-java/issues/931\n+ */\n+public class GotchasActivity extends Activity {\n+\n+\n+    private Realm realm;\n+    private Subscription subscription;\n+    private ViewGroup container;\n+\n+    @Override\n+    protected void onCreate(Bundle savedInstanceState) {\n+        super.onCreate(savedInstanceState);\n+        setContentView(R.layout.activity_gotchas);\n+        container = (ViewGroup) findViewById(R.id.list);\n+        realm = Realm.getDefaultInstance();\n+    }\n+\n+    @Override\n+    protected void onResume() {\n+        super.onResume();\n+\n+        Subscription distinctSubscription = testDistinct();\n+        Subscription bufferSubscription = testBuffer();\n+        Subscription subscribeOnSubscription = testSubscribeOn();\n+\n+        // Trigger updates\n+        realm.executeTransaction(new Realm.Transaction() {\n+            @Override\n+            public void execute(Realm realm) {\n+                realm.allObjectsSorted(Person.class, \"name\", Sort.ASCENDING).get(0).setAge(new Random().nextInt(100));\n+            }\n+        });\n+\n+        subscription = new CompositeSubscription(\n+                distinctSubscription,\n+                bufferSubscription,\n+                subscribeOnSubscription\n+        );\n+    }\n+\n+    /**\n+     * Shows how to be careful with `subscribeOn()`\n+     */\n+    private Subscription testSubscribeOn() {\n+        Subscription subscribeOn = realm.asObservable().map(new Func1<Realm, Person>() {\n+            @Override\n+            public Person call(Realm realm) {\n+                // The Realm object was created on the UI thread. Accessing it on `Schedulers.io()` will crash.\n+                // Avoid using subscribeOn() and use Realms `findAllAsync*()` methods instead.\n+                return realm.where(Person.class).findAllSorted(\"name\").get(0);\n+            }\n+        })\n+                .subscribeOn(Schedulers.io())","path":"examples/rxJavaExample/src/main/java/io/realm/examples/rxjava/gotchas/GotchasActivity.java","position":104,"original_position":104,"commit_id":"aa6bfc7404c051e4083522ece65c4bc7240ded87","original_commit_id":"aa6bfc7404c051e4083522ece65c4bc7240ded87","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":"should we add a reminder warning as a comment to highlight the _DO NOT DO THIS_ ","created_at":"2015-12-16T19:17:29Z","updated_at":"2015-12-16T19:17:29Z","html_url":"https://github.com/realm/realm-java/pull/1940#discussion_r47820375","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/1940","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/47820375"},"html":{"href":"https://github.com/realm/realm-java/pull/1940#discussion_r47820375"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1940"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/1940","id":53577501,"html_url":"https://github.com/realm/realm-java/pull/1940","diff_url":"https://github.com/realm/realm-java/pull/1940.diff","patch_url":"https://github.com/realm/realm-java/pull/1940.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/1940","number":1940,"state":"open","locked":false,"title":"Adding RxJava example","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":"Part of #1710 \r\nThis PR replaces https://github.com/realm/realm-java/pull/1870\r\n\r\nIt adds a new example project showing how Realm can interop with RxJava, especially it highlights some of our current caveats and show work-arounds.","created_at":"2015-12-14T15:18:34Z","updated_at":"2015-12-16T19:17:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"307ff8787516a5951786097566b6095cb08da99f","assignee":{"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},"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/1940/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/1940/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/1940/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/aa6bfc7404c051e4083522ece65c4bc7240ded87","head":{"label":"realm:cm/feature-rxjava-example","ref":"cm/feature-rxjava-example","sha":"aa6bfc7404c051e4083522ece65c4bc7240ded87","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-12-16T15:33:37Z","pushed_at":"2015-12-16T18:42:22Z","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":43460,"stargazers_count":3109,"watchers_count":3109,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":342,"mirror_url":null,"open_issues_count":184,"forks":342,"open_issues":184,"watchers":3109,"default_branch":"master"}},"base":{"label":"realm:feature-rxjava","ref":"feature-rxjava","sha":"36c7c3f64c4acc155bcd9e3bc060aee0a8106ac0","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-12-16T15:33:37Z","pushed_at":"2015-12-16T18:42:22Z","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":43460,"stargazers_count":3109,"watchers_count":3109,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":342,"mirror_url":null,"open_issues_count":184,"forks":342,"open_issues":184,"watchers":3109,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1940"},"html":{"href":"https://github.com/realm/realm-java/pull/1940"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/1940"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/1940/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1940/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/1940/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/aa6bfc7404c051e4083522ece65c4bc7240ded87"}}}}