{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/2122","labels_url":"https://api.github.com/repos/realm/realm-java/issues/2122/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/2122/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/2122/events","html_url":"https://github.com/realm/realm-java/issues/2122","id":127697897,"number":2122,"title":"Use rx java not receive the RealmResult change if not hold the result instance, so what is the best practice working on RX?","user":{"login":"luhui","id":2842522,"avatar_url":"https://avatars.githubusercontent.com/u/2842522?v=3","gravatar_id":"","url":"https://api.github.com/users/luhui","html_url":"https://github.com/luhui","followers_url":"https://api.github.com/users/luhui/followers","following_url":"https://api.github.com/users/luhui/following{/other_user}","gists_url":"https://api.github.com/users/luhui/gists{/gist_id}","starred_url":"https://api.github.com/users/luhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luhui/subscriptions","organizations_url":"https://api.github.com/users/luhui/orgs","repos_url":"https://api.github.com/users/luhui/repos","events_url":"https://api.github.com/users/luhui/events{/privacy}","received_events_url":"https://api.github.com/users/luhui/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/Help","name":"Help","color":"cc317c"},{"url":"https://api.github.com/repos/realm/realm-java/labels/Pending","name":"Pending","color":"ededed"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-01-20T14:46:58Z","updated_at":"2016-01-21T02:08:40Z","closed_at":"2016-01-21T02:08:40Z","body":"When we use findAsync and asObservable to create a fetch observable, it will not receive the change if we do not instance the RealmResult in the first onNext called.\r\n\r\nSee the code below:\r\n\r\nOnce we delete the doOnNext method, the realmResults will be release\r\n```java\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    private Realm realm;\r\n    private RealmResults realmResults;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        Realm.setDefaultConfiguration(new RealmConfiguration.Builder(getApplicationContext()).deleteRealmIfMigrationNeeded().build());\r\n        setContentView(R.layout.activity_main);\r\n\r\n        //observable\r\n        realm = Realm.getDefaultInstance();\r\n        realm\r\n                .where(Users.class)\r\n                .findAllAsync()\r\n                .asObservable()\r\n                .doOnNext(new Action1<RealmResults<Users>>() {\r\n                    @Override\r\n                    public void call(RealmResults<Users> userses) {\r\n                        realmResults = userses; //if delete this line, it will receive the data change when create or delete data.\r\n                    }\r\n                })\r\n                .filter(new Func1<RealmResults<Users>, Boolean>() {\r\n                    @Override\r\n                    public Boolean call(RealmResults<Users> userses) {\r\n                        return userses.isLoaded();\r\n                    }\r\n                })\r\n                .filter(new Func1<RealmResults<Users>, Boolean>() {\r\n                    @Override\r\n                    public Boolean call(RealmResults<Users> userses) {\r\n                        Log.e(\"lh\", \"is empty???\" + userses.isEmpty());\r\n                        return !userses.isEmpty();\r\n                    }\r\n                })\r\n                .subscribe(new Action1<RealmResults<Users>>() {\r\n                    @Override\r\n                    public void call(RealmResults<Users> userses) {\r\n                        Log.e(\"lh\", \"on change??? \" + userses.first());\r\n                    }\r\n                });\r\n\r\n\r\n        //data\r\n        realm.executeTransaction(new Realm.Transaction() {\r\n            @Override\r\n            public void execute(Realm realm) {\r\n                //create a user data\r\n                Users users = new Users();\r\n                users.setId(UUID.randomUUID().toString());\r\n                users.setName(String.valueOf(System.currentTimeMillis()));\r\n                realm.copyToRealmOrUpdate(users);\r\n                Log.e(\"lh\", \"did copy\");\r\n            }\r\n        }, new Realm.Transaction.Callback() {\r\n            @Override\r\n            public void onSuccess() {\r\n                super.onSuccess();\r\n                //delay 1s to delete\r\n                new CountDownTimer(1000, 1000) {\r\n                    @Override\r\n                    public void onTick(long millisUntilFinished) {\r\n\r\n                    }\r\n\r\n                    @Override\r\n                    public void onFinish() {\r\n                        //delete to data just created\r\n                        realm.executeTransaction(new Realm.Transaction() {\r\n                            @Override\r\n                            public void execute(Realm realm) {\r\n                                Users users = realm.where(Users.class).findFirst();\r\n                                users.removeFromRealm();\r\n                            }\r\n                        });\r\n                    }\r\n                }.start();\r\n            }\r\n        });\r\n    }\r\n}\r\n```\r\n\r\nIn HandlerController, when the controller try to notify the listener, get the realmResult is null\r\n\r\n```java\r\nprivate void notifyRealmResultsCallbacks(Iterator<WeakReference<RealmResults<? extends RealmObject>>> iterator) {\r\n        List<RealmResults<? extends RealmObject>> resultsToBeNotified =\r\n                new ArrayList<RealmResults<? extends RealmObject>>();\r\n        while (iterator.hasNext()) {\r\n            WeakReference<RealmResults<? extends RealmObject>> weakRealmResults = iterator.next();\r\n            //get null here, the RealmResult is being released\r\n            RealmResults<? extends RealmObject> realmResults = weakRealmResults.get();\r\n            if (realmResults == null) {\r\n                iterator.remove();\r\n            } else {\r\n                // It should be legal to modify asyncRealmResults and syncRealmResults in the listener\r\n                resultsToBeNotified.add(realmResults);\r\n            }\r\n        }\r\n\r\n        for (RealmResults<? extends RealmObject> realmResults : resultsToBeNotified) {\r\n            realmResults.notifyChangeListeners();\r\n        }\r\n    }\r\n```\r\n\r\nSo, what is the best practice on working with RXJava? \r\nDid I have to handle every RealmResult instance?"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/173428109","html_url":"https://github.com/realm/realm-java/issues/2122#issuecomment-173428109","issue_url":"https://api.github.com/repos/realm/realm-java/issues/2122","id":173428109,"user":{"login":"luhui","id":2842522,"avatar_url":"https://avatars.githubusercontent.com/u/2842522?v=3","gravatar_id":"","url":"https://api.github.com/users/luhui","html_url":"https://github.com/luhui","followers_url":"https://api.github.com/users/luhui/followers","following_url":"https://api.github.com/users/luhui/following{/other_user}","gists_url":"https://api.github.com/users/luhui/gists{/gist_id}","starred_url":"https://api.github.com/users/luhui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luhui/subscriptions","organizations_url":"https://api.github.com/users/luhui/orgs","repos_url":"https://api.github.com/users/luhui/repos","events_url":"https://api.github.com/users/luhui/events{/privacy}","received_events_url":"https://api.github.com/users/luhui/received_events","type":"User","site_admin":false},"created_at":"2016-01-21T02:08:40Z","updated_at":"2016-01-21T02:08:40Z","body":"Oh, it's my mistake, hold the subscription or use takeUntil would be work.\r\n\r\n\r\n```java\r\n//hold the subscription and unsubscribe it whenever it needed\r\nsubscription = realm.where(Users.class)\r\n  .findAllAsync()\r\n  .asObservable()\r\n  .filter(results::isLoaded())\r\n  .filter(!users::isEmpty())\r\n//subscribe the data change and update view\r\n````\r\n\r\nor just use `takeUntil`\r\n```java\r\n//hold the subscription and unsubscribe it whenever it needed\r\nrealm.where(Users.class)\r\n  .findAllAsync()\r\n  .asObservable()\r\n  .filter(results::isLoaded())\r\n  .filter(!users::isEmpty())\r\n  .takeUntil(stopObservable)\r\n//subscribe the data change and update view\r\n````\r\n\r\n@cmelchior  Thanks for your help：）"}}