{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/2485","repository_url":"https://api.github.com/repos/realm/realm-java","labels_url":"https://api.github.com/repos/realm/realm-java/issues/2485/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/2485/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/2485/events","html_url":"https://github.com/realm/realm-java/issues/2485","id":141298143,"number":2485,"title":"RealmResults::asObservable not always returns a value when subscription is not kept in memory","user":{"login":"AranHase","id":1572068,"avatar_url":"https://avatars.githubusercontent.com/u/1572068?v=3","gravatar_id":"","url":"https://api.github.com/users/AranHase","html_url":"https://github.com/AranHase","followers_url":"https://api.github.com/users/AranHase/followers","following_url":"https://api.github.com/users/AranHase/following{/other_user}","gists_url":"https://api.github.com/users/AranHase/gists{/gist_id}","starred_url":"https://api.github.com/users/AranHase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AranHase/subscriptions","organizations_url":"https://api.github.com/users/AranHase/orgs","repos_url":"https://api.github.com/users/AranHase/repos","events_url":"https://api.github.com/users/AranHase/events{/privacy}","received_events_url":"https://api.github.com/users/AranHase/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/S:P1%20Backlog","name":"S:P1 Backlog","color":"dedede"},{"url":"https://api.github.com/repos/realm/realm-java/labels/T:Bug","name":"T:Bug","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-03-16T14:52:56Z","updated_at":"2016-03-17T12:46:35Z","closed_at":null,"body":"#### Goal\r\n\r\n> What do you want to achieve?\r\n\r\nDo a lot of simple realm queries with `findAllAsync().asObservable()` and have them all return a value, even without explicitly holding a reference to its subscription.\r\n\r\n(or add a note in the docs about the importance of always holding a reference to the subscriptions? for the noobs like me...)\r\n\r\n#### Expected Results\r\n\r\nWhenever we create an observable with `findAllAsync().asObservable()`, it would be nice to always get a `RealmResults`, even when we don't hold a reference to the subscription.\r\n\r\n#### Actual Results\r\n\r\nSome observables created with `findAllAsync().asObservable()` will never call the `onNext(...)` of the subscription.\r\n\r\n#### Steps & Code to Reproduce\r\n\r\nI created a small Android app showing the problem I'm facing on a bigger application.\r\n\r\nThe important code is on this line:\r\nhttps://github.com/AranHase/TestFindAllAsync/blob/13ba8e68d9b9061825125ff3475c6d7091aa93bf/app/src/main/java/com/example/hasegawa/testfindallasync/MainActivity.kt#L84\r\n\r\nIn this test, one thousand \"Realm observables\" are created. Every time one observable is created, a counter gets incremented. For each of those observable, one subscription is created (and may or may not be explicitly stored in memory). When this subscription returns any value, another counter is incremented.\r\n\r\nWhen the subscription is stored in memory, then we get the expected behavior (one thousand observables created, and one thousand results). However, when we do not store the subscription in memory, the number of results may not be one thousand.\r\n\r\nThis line determines if the subscription should be kept in memory or not, and it will change the behavior of the program:\r\nhttps://github.com/AranHase/TestFindAllAsync/blob/13ba8e68d9b9061825125ff3475c6d7091aa93bf/app/src/main/java/com/example/hasegawa/testfindallasync/MainActivity.kt#L106\r\n\r\nI also tried a `flatMap` approach in here:\r\nhttps://github.com/AranHase/TestFindAllAsync/blob/13ba8e68d9b9061825125ff3475c6d7091aa93bf/app/src/main/java/com/example/hasegawa/testfindallasync/MainActivity.kt#L114\r\n\r\nHowever, `flatMap` fails with: \r\n\r\n                                                                                       java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@b297227 rejected from io.realm.internal.async.RealmThreadPoolExecutor@873a166[Running, pool size = 5, active threads = 5, queued tasks = 100, completed tasks = 125]\r\n                                                                                                 at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2014)\r\n                                                                                                 at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:794)\r\n                                                                                                 at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1340)\r\n                                                                                                 at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:104)\r\n                                                                                                 at io.realm.internal.async.RealmThreadPoolExecutor.submit(RealmThreadPoolExecutor.java:68)\r\n                                                                                                 at io.realm.RealmQuery.findAllAsync(RealmQuery.java:1470)\r\n                                                                                                 at com.example.hasegawa.testfindallasync.MainActivity$onClick$1.call(MainActivity.kt:90)\r\n                                                                                                 at com.example.hasegawa.testfindallasync.MainActivity$onClick$1.call(MainActivity.kt:19)\r\n                                                                                                 at rx.Observable$29.onNext(Observable.java:8031)\r\n                                                                                                 at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)\r\n                                                                                                 at rx.internal.operators.OnSubscribeRange$RangeProducer.fastpath(OnSubscribeRange.java:126)\r\n                                                                                                 at rx.internal.operators.OnSubscribeRange$RangeProducer.request(OnSubscribeRange.java:63)\r\n                                                                                                 at rx.Subscriber.setProducer(Subscriber.java:209)\r\n                                                                                                 at rx.Subscriber.setProducer(Subscriber.java:205)\r\n                                                                                                 at rx.internal.operators.OnSubscribeRange.call(OnSubscribeRange.java:38)\r\n                                                                                                 at rx.internal.operators.OnSubscribeRange.call(OnSubscribeRange.java:26)\r\n                                                                                                 at rx.Observable.subscribe(Observable.java:8191)\r\n                                                                                                 at rx.Observable.subscribe(Observable.java:8158)\r\n                                                                                                 at rx.Observable.subscribe(Observable.java:8017)\r\n                                                                                                 at com.example.hasegawa.testfindallasync.MainActivity.onClick(MainActivity.kt:85)\r\n                                                                                                 at android.view.View.performClick(View.java:5198)\r\n                                                                                                 at android.view.View$PerformClick.run(View.java:21147)\r\n                                                                                                 at android.os.Handler.handleCallback(Handler.java:739)\r\n                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)\r\n                                                                                                 at android.os.Looper.loop(Looper.java:148)\r\n                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)\r\n                                                                                                 at java.lang.reflect.Method.invoke(Native Method)\r\n                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)\r\n                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)\r\n\r\n#### Version of Realm and tooling\r\nRealm version(s): 0.88.1\r\n\r\nAndroid Studio version: 2.1 Preview 1\r\n\r\nWhich Android version and device: Nexus 5 API 23 (emulator)\r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/197862078","html_url":"https://github.com/realm/realm-java/issues/2485#issuecomment-197862078","issue_url":"https://api.github.com/repos/realm/realm-java/issues/2485","id":197862078,"user":{"login":"AranHase","id":1572068,"avatar_url":"https://avatars.githubusercontent.com/u/1572068?v=3","gravatar_id":"","url":"https://api.github.com/users/AranHase","html_url":"https://github.com/AranHase","followers_url":"https://api.github.com/users/AranHase/followers","following_url":"https://api.github.com/users/AranHase/following{/other_user}","gists_url":"https://api.github.com/users/AranHase/gists{/gist_id}","starred_url":"https://api.github.com/users/AranHase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AranHase/subscriptions","organizations_url":"https://api.github.com/users/AranHase/orgs","repos_url":"https://api.github.com/users/AranHase/repos","events_url":"https://api.github.com/users/AranHase/events{/privacy}","received_events_url":"https://api.github.com/users/AranHase/received_events","type":"User","site_admin":false},"created_at":"2016-03-17T12:46:34Z","updated_at":"2016-03-17T12:46:34Z","body":"Hey @cmelchior , thank you for the observations. Now I understand the problem with the `flatMap` approach very clearly, and should be straightforward to avoid it (like using `concatMap` instead)."}}