{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/2990","repository_url":"https://api.github.com/repos/realm/realm-java","labels_url":"https://api.github.com/repos/realm/realm-java/issues/2990/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/2990/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/2990/events","html_url":"https://github.com/realm/realm-java/pull/2990","id":159915888,"number":2990,"title":"Race condition between Realm change notifications and UI events","user":{"login":"cmelchior","id":406066,"avatar_url":"https://avatars.githubusercontent.com/u/406066?v=3","gravatar_id":"","url":"https://api.github.com/users/cmelchior","html_url":"https://github.com/cmelchior","followers_url":"https://api.github.com/users/cmelchior/followers","following_url":"https://api.github.com/users/cmelchior/following{/other_user}","gists_url":"https://api.github.com/users/cmelchior/gists{/gist_id}","starred_url":"https://api.github.com/users/cmelchior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmelchior/subscriptions","organizations_url":"https://api.github.com/users/cmelchior/orgs","repos_url":"https://api.github.com/users/cmelchior/repos","events_url":"https://api.github.com/users/cmelchior/events{/privacy}","received_events_url":"https://api.github.com/users/cmelchior/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/S:Review","name":"S:Review","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-06-13T10:10:37Z","updated_at":"2016-06-15T15:23:48Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/2990","html_url":"https://github.com/realm/realm-java/pull/2990","diff_url":"https://github.com/realm/realm-java/pull/2990.diff","patch_url":"https://github.com/realm/realm-java/pull/2990.patch"},"body":"Fixes realm/realm-android-adapters#11\r\nFixes realm/realm-android-adapters#48\r\n\r\nFrom #2952 \r\n\r\nThis PR fixes a bug surfaced by our Realm Android Adapter example. Before this PR we posted all REALM_CHANGED events to the looper queue as normal. This meant that potential touch input events could get in front which in turn could cause a relayout of a ListView which would then crash because it detected a difference between it's cached adapter size and the actual one.\r\n\r\nThis PR now forces all local commits to be placed at the front of the queue eliminating that risk.\r\n\r\nUsing postAtFrontOfQueue is highly discouraged by Google since you risk starving the UI thread and introduce lag. However commits are generally fairly infrequent and any loop (commits inside a change listener) will now be more easily detected. Before this, they would probably work, although making the UI sluggish.\r\n\r\nImplementation is as follows:\r\n\r\n| Things need to do | REALM_CHANGED | LOCAL_COMMIT |\r\n| ---               |  ---          |  ---         |\r\n| Finished async query | Swallow the event and schedule a requery | advance_read/sync them on the UI thread |\r\n| Unfinished async query | Swallow the event and schedule a requery | Ignore |\r\n| Sync synced `RealmResults` | YES | YES |\r\n| advance read | YES | YES |\r\n| Notify global listeners | YES | YES |\r\n| Notify typed listeners | YES | YES (only loaded RealmResults/Objects) |\r\n\r\nImplementation comment:\r\nIdeally on LOCAL_COMMIT we want to stop any non-loaded queries as they will fail when reaching the Looper and will need to retry anyway. For async queries that are already loaded, we don't need to reschedule as they will be synced on the Looper thread. However currently it is not possible to make that distinction, and I would rather keep this out of scope for this PR, as it would be a bigger change for little benefit.\r\n\r\nFor that reason I am instead just letting any un-loaded queries run to completion including retry."},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/226222215","html_url":"https://github.com/realm/realm-java/pull/2990#issuecomment-226222215","issue_url":"https://api.github.com/repos/realm/realm-java/issues/2990","id":226222215,"user":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"created_at":"2016-06-15T15:23:48Z","updated_at":"2016-06-15T15:23:48Z","body":"LGTM small comment 👍 "}}
{"actor":{"display_login":"nhachicha"}}