{"action":"opened","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/974","labels_url":"https://api.github.com/repos/realm/realm-java/issues/974/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/974/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/974/events","html_url":"https://github.com/realm/realm-java/issues/974","id":62425335,"number":974,"title":"Iteration throughs RealmResults and changes elements problem","user":{"login":"lirik16","id":11039908,"avatar_url":"https://avatars.githubusercontent.com/u/11039908?v=3","gravatar_id":"","url":"https://api.github.com/users/lirik16","html_url":"https://github.com/lirik16","followers_url":"https://api.github.com/users/lirik16/followers","following_url":"https://api.github.com/users/lirik16/following{/other_user}","gists_url":"https://api.github.com/users/lirik16/gists{/gist_id}","starred_url":"https://api.github.com/users/lirik16/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lirik16/subscriptions","organizations_url":"https://api.github.com/users/lirik16/orgs","repos_url":"https://api.github.com/users/lirik16/repos","events_url":"https://api.github.com/users/lirik16/events{/privacy}","received_events_url":"https://api.github.com/users/lirik16/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-17T15:18:03Z","updated_at":"2015-03-17T15:18:03Z","closed_at":null,"body":"Hi guys! \r\n\r\nI have a bug in the the process of changing the RealmResults elements. I execute the following line:\r\n\r\nfinal RealmResults<RealmContact> result = realm.where(RealmContact.class).equalTo(\"sync\", false).findAll();\r\n\r\nI get the RealmResult objects and try to change its elements:\r\nVersion 1\r\n\r\nrealm.beginTransaction();\r\nfor (Contact contact : result) {\r\n    contact.setSync(true);\r\n}\r\nrealm.commitTransaction();\r\n\r\nIf I do it I get the ConcurrentModificationException. I tried to do the way shown below^\r\nVersion 2\r\n\r\nrealm.beginTransaction();\r\nfor (int i = 0, size = result.size(); i < size; ++i) {\r\n    result.get(i).setSync(true);\r\n}\r\nrealm.commitTransaction();\r\n\r\nI get the ArrayIndexOutOfBoundsException. If I do it this way\r\nVersion 3\r\n\r\nrealm.beginTransaction();\r\nfor (int i = 0; i < result.size(); ++i) {\r\n    result.get(i).setSync(true);\r\n}\r\nrealm.commitTransaction();\r\n\r\nThe code finished without exception! When setSync(true) is executed the size of RealmResult decreases by one. So if the RealmResult contains 9 elements, and I use Version 3 it changes the value of just half of the elements. The changes occure before we call realm.commitTransaction() that's why we get ArrayIndexOutOfBoundsException and ConcurrentModificationException. \r\n\r\nThat is a very strange behavior, and it looks like a bug. In that case in order to change the value of all RealmResult elements I have to use the following code \r\n\r\nrealm.beginTransaction();\r\nfor (int i = 0; i < result.size(); ++i) {\r\n    result.get(0).setSync(true);\r\n}\r\nrealm.commitTransaction();"}}