{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/3147","repository_url":"https://api.github.com/repos/realm/realm-java","labels_url":"https://api.github.com/repos/realm/realm-java/issues/3147/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/3147/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/3147/events","html_url":"https://github.com/realm/realm-java/issues/3147","id":164060381,"number":3147,"title":"java.lang.IllegalStateException: when use findAll().asObservable() with subscribeOn(Schedulers.io()). Similar to #2607","user":{"login":"ar-g","id":3674446,"avatar_url":"https://avatars.githubusercontent.com/u/3674446?v=3","gravatar_id":"","url":"https://api.github.com/users/ar-g","html_url":"https://github.com/ar-g","followers_url":"https://api.github.com/users/ar-g/followers","following_url":"https://api.github.com/users/ar-g/following{/other_user}","gists_url":"https://api.github.com/users/ar-g/gists{/gist_id}","starred_url":"https://api.github.com/users/ar-g/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ar-g/subscriptions","organizations_url":"https://api.github.com/users/ar-g/orgs","repos_url":"https://api.github.com/users/ar-g/repos","events_url":"https://api.github.com/users/ar-g/events{/privacy}","received_events_url":"https://api.github.com/users/ar-g/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/T:Help","name":"T:Help","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-07-06T12:04:25Z","updated_at":"2016-07-07T11:28:44Z","closed_at":null,"body":"I'm getting this exception( Caused by: java.lang.IllegalStateException: You can't register a listener from a non-Looper thread) when executing this code\r\n```java\r\nreturn Observable.defer(() -> Observable.just(Realm.getInstance(realmConfiguration)))\r\n        .flatMap(realm -> realm.where(clazz)\r\n            .findAll()\r\n            .asObservable()\r\n            .filter(RealmResults::isLoaded)\r\n            .map(sort)\r\n            .map(realm::copyFromRealm)\r\n            .doOnUnsubscribe(realm::close)\r\n        );\r\n```\r\nMy use case is to fetch data from Realm and then make NetworkRequest all this with `subscribeOn(Schedulers.io())`. What is the problem here?"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/231052536","html_url":"https://github.com/realm/realm-java/issues/3147#issuecomment-231052536","issue_url":"https://api.github.com/repos/realm/realm-java/issues/3147","id":231052536,"user":{"login":"Zhuinden","id":11718392,"avatar_url":"https://avatars.githubusercontent.com/u/11718392?v=3","gravatar_id":"","url":"https://api.github.com/users/Zhuinden","html_url":"https://github.com/Zhuinden","followers_url":"https://api.github.com/users/Zhuinden/followers","following_url":"https://api.github.com/users/Zhuinden/following{/other_user}","gists_url":"https://api.github.com/users/Zhuinden/gists{/gist_id}","starred_url":"https://api.github.com/users/Zhuinden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zhuinden/subscriptions","organizations_url":"https://api.github.com/users/Zhuinden/orgs","repos_url":"https://api.github.com/users/Zhuinden/repos","events_url":"https://api.github.com/users/Zhuinden/events{/privacy}","received_events_url":"https://api.github.com/users/Zhuinden/received_events","type":"User","site_admin":false},"created_at":"2016-07-07T11:28:44Z","updated_at":"2016-07-07T11:28:44Z","body":"Actually, I started looking into your problem, and it's actually not as complicated to figure out as I thought.\r\n\r\nThe `RealmObservableFactory` attempts to register a change listener when you call `asObservable()` with it, this is the code\r\n\r\n``` java\r\n    @Override\r\n    public Observable<Realm> from(Realm realm) {\r\n        final RealmConfiguration realmConfig = realm.getConfiguration();\r\n        return Observable.create(new Observable.OnSubscribe<Realm>() {\r\n            @Override\r\n            public void call(final Subscriber<? super Realm> subscriber) {\r\n                // Get instance to make sure that the Realm is open for as long as the\r\n                // Observable is subscribed to it.\r\n                final Realm observableRealm = Realm.getInstance(realmConfig);\r\n                final RealmChangeListener<Realm> listener = new RealmChangeListener<Realm>() {\r\n                    @Override\r\n                    public void onChange(Realm realm) {\r\n                        if (!subscriber.isUnsubscribed()) {\r\n                            subscriber.onNext(observableRealm);\r\n                        }\r\n                    }\r\n                };\r\n                observableRealm.addChangeListener(listener);\r\n                subscriber.add(Subscriptions.create(new Action0() {\r\n                    @Override\r\n                    public void call() {\r\n                        observableRealm.removeChangeListener(listener);\r\n                        observableRealm.close();\r\n                    }\r\n                }));\r\n                subscriber.onNext(observableRealm);\r\n            }\r\n        });\r\n    }\r\n```\r\n\r\nAnd this line\r\n\r\n            observableRealm.addChangeListener(listener)\r\n\r\nWorks only if the Thread you're on supports auto-update, which the `Schedulers.io()` does not (it is not a Looper thread).\r\n\r\nI guess a possible solution would be to either provide a different observable factory that doesn't try to add a change listener if the realm returns `realm.isAutoRefresh()` is true."}}
{"actor":{"display_login":"Zhuinden"}}