{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/3788","repository_url":"https://api.github.com/repos/realm/realm-java","labels_url":"https://api.github.com/repos/realm/realm-java/issues/3788/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/3788/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/3788/events","html_url":"https://github.com/realm/realm-java/issues/3788","id":189089901,"number":3788,"title":"Realm Android - Multiple Threads, updating objects not working at all...","user":{"login":"heitorpr","id":6535255,"avatar_url":"https://avatars.githubusercontent.com/u/6535255?v=3","gravatar_id":"","url":"https://api.github.com/users/heitorpr","html_url":"https://github.com/heitorpr","followers_url":"https://api.github.com/users/heitorpr/followers","following_url":"https://api.github.com/users/heitorpr/following{/other_user}","gists_url":"https://api.github.com/users/heitorpr/gists{/gist_id}","starred_url":"https://api.github.com/users/heitorpr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heitorpr/subscriptions","organizations_url":"https://api.github.com/users/heitorpr/orgs","repos_url":"https://api.github.com/users/heitorpr/repos","events_url":"https://api.github.com/users/heitorpr/events{/privacy}","received_events_url":"https://api.github.com/users/heitorpr/received_events","type":"User","site_admin":false},"labels":[{"id":3545600,"url":"https://api.github.com/repos/realm/realm-java/labels/T:Help","name":"T:Help","color":"cc317c","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-11-14T11:30:59Z","updated_at":"2016-11-14T12:05:32Z","closed_at":null,"body":"> What do you want to achieve?\r\nFetch an object from Realm on one instance and another has the latest update.\r\n\r\n#### Expected Results\r\n1 - Thread 1 create object with primary key\r\n2 - Fetch the object on Thread 2 and the object is returned\r\n\r\n#### Actual Results\r\n1 - Thread 2 bring null object =(\r\n\r\n#### Steps & Code to Reproduce\r\n1 - Init realm on application class\r\n\r\n        Realm.init(this);\r\n        realmConfiguration = new RealmConfiguration.Builder()\r\n                .schemaVersion(17)\r\n                .migration(new RunrunitMigration())\r\n                .build();\r\n\r\n        Realm.setDefaultConfiguration(realmConfiguration);\r\n        Realm realm = Realm.getDefaultInstance();\r\n        realm.setAutoRefresh(true);\r\n\r\n2 - Create object and update it on main thread. The object is created with UUID this way:\r\n\r\nClient example\r\n\r\n    public static Client create() {\r\n        return new Client(create(new RealmClient(UUID.randomUUID().toString())));\r\n    }\r\n\r\nOn Model Generic Class:\r\n\r\n    public static <T extends RealmObject> T create(T object) {\r\n        Realm realm = Realm.getDefaultInstance();\r\n        realm.beginTransaction();\r\n        object = realm.copyToRealmOrUpdate(object);\r\n        realm.commitTransaction();\r\n        realm.close();\r\n\r\n        return object;\r\n    }\r\n\r\n3 - to save the object on updates I do the following code:\r\n\r\n    public void save() {\r\n        if (valuesToUpdate.isEmpty())\r\n            return;\r\n\r\n        Realm realm = Realm.getDefaultInstance();\r\n        realm.executeTransaction(new Realm.Transaction() {\r\n            @Override\r\n            public void execute(Realm realm) {\r\n                doSave(model);\r\n            }\r\n        });\r\n    }\r\n\r\n4 - To send the object I call a method that is async sending the UUID to the job, with that I can fetch the realm object there, this way:\r\n\r\n    public T find(String key, Object objId) {\r\n        Realm realm = null;\r\n        T realmObject = null;\r\n\r\n        if (objId != null) {\r\n            try {\r\n                realm = Realm.getDefaultInstance();\r\n                realmObject = realm.where(type).equalTo(key, (String) objId).findFirst();\r\n            } catch (Exception ex) {\r\n                Utils.logException(getClass().getSimpleName() + \":find:\" + objId, ex);\r\n            } finally {\r\n                if (realm != null) {\r\n                    realm.close();\r\n                }\r\n            }\r\n        }\r\n\r\n        this.model = realmObject;\r\n        return realmObject;\r\n    }\r\n\r\n5 - In the end... my object is null.. Before updating the realm, I was using realm.refresh() to obtain the object on the async task.\r\n\r\n6 - Well, I really do not know what to do to solve this... Tried to create a new realmObject with the same UUID on the async task, and it works... But, should work? =(, I'm confused and sad! Thought the realm objects was updated across threads... Don't know what I'm doing wrong, but my entire app when creating or updating objects use this logic =/. Some help plz! =D\r\n\r\n7 - Observation: I am receiving this warning:\r\n\r\nREALM_JAVA: Mixing asynchronous queries with local writes should be avoided. Realm will convert any async queries to synchronous in order to remain consistent. Use asynchronous writes instead.\r\n\r\n#### Version of Realm and tooling\r\nRealm version(s): ? \r\n2.1.1\r\n\r\nRealm sync feature enabled: yes/no\r\nyes\r\n\r\nAndroid Studio version: ?\r\n2.2.2\r\n\r\nWhich Android version and device: ?\r\n6.0\r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/260318809","html_url":"https://github.com/realm/realm-java/issues/3788#issuecomment-260318809","issue_url":"https://api.github.com/repos/realm/realm-java/issues/3788","id":260318809,"user":{"login":"heitorpr","id":6535255,"avatar_url":"https://avatars.githubusercontent.com/u/6535255?v=3","gravatar_id":"","url":"https://api.github.com/users/heitorpr","html_url":"https://github.com/heitorpr","followers_url":"https://api.github.com/users/heitorpr/followers","following_url":"https://api.github.com/users/heitorpr/following{/other_user}","gists_url":"https://api.github.com/users/heitorpr/gists{/gist_id}","starred_url":"https://api.github.com/users/heitorpr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heitorpr/subscriptions","organizations_url":"https://api.github.com/users/heitorpr/orgs","repos_url":"https://api.github.com/users/heitorpr/repos","events_url":"https://api.github.com/users/heitorpr/events{/privacy}","received_events_url":"https://api.github.com/users/heitorpr/received_events","type":"User","site_admin":false},"created_at":"2016-11-14T12:05:31Z","updated_at":"2016-11-14T12:05:31Z","body":"1 - The find method get a realm instance just for him, but the activity or thread that called it, has it own instance (on same thread) following activity, fragment or job life cycle. It kind works =).\r\n\r\ndoSave(model) do not talks with worker thread, it is local too.\r\n\r\n    private void doSave(RealmProject model) {\r\n        if (model == null) return;\r\n\r\n        for (Map.Entry<String, Object> entry : valuesToUpdate.entrySet()) {\r\n            String key = entry.getKey();\r\n            Object value = entry.getValue();\r\n\r\n            switch (key) {\r\n                case \"id\":\r\n                    model.setId(Utils.parseString(value));\r\n                    break;\r\n                case \"name\":\r\n                    model.setName(Utils.parseString(value));\r\n                    break;\r\n \r\n                ...\r\n                case \"activities\":\r\n                    model.setActivities(Utils.parseLong(value));\r\n                    break;\r\n            }\r\n        }\r\n    }\r\n\r\n2 - I checked before calling the worker thread, my object is updated before going to the new thread. And there my object is not created. Also without using my \"find()\" method, using direct the realm way, my object is not created also.\r\n\r\n3 - Observation.. I have a java object called Model that has a RealmModel, so on Model I do all operations and interfaces to save,update,fetch the realmModel. And on realmModel it just contains data from the database, no methods there.\r\n\r\n4 - Getting null pointer on worker thread before calling realm when tried to run `Realm.getLocalInstanceCount(realmConfiguration)`:\r\n\r\n> java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference\r\n\r\nOn RealmCache.java -- > RealmCacheType.values() returns 2 values, (the second is DYNAMIC_REALM, null pointer happens inside the for loop on line 405.\r\n\r\n    static int getLocalThreadCount(RealmConfiguration configuration) {\r\n        RealmCache cache = cachesMap.get(configuration.getPath());\r\n        if (cache == null) {\r\n            return 0;\r\n        } else {\r\n            int totalRefCount = 0;\r\n            for (RealmCacheType type : RealmCacheType.values()) {\r\n                totalRefCount += cache.refAndCountMap.get(type).localCount.get();\r\n            }\r\n            return totalRefCount;\r\n        }\r\n    }\r\n"}}
{"actor":{"display_login":"heitorpr"}}