{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097/events","html_url":"https://github.com/realm/realm-cocoa/issues/1097","id":47613486,"number":1097,"title":"Confine on dispatch queue rather than thread","user":{"login":"rogernolan","id":566487,"avatar_url":"https://avatars.githubusercontent.com/u/566487?v=3","gravatar_id":"","url":"https://api.github.com/users/rogernolan","html_url":"https://github.com/rogernolan","followers_url":"https://api.github.com/users/rogernolan/followers","following_url":"https://api.github.com/users/rogernolan/following{/other_user}","gists_url":"https://api.github.com/users/rogernolan/gists{/gist_id}","starred_url":"https://api.github.com/users/rogernolan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogernolan/subscriptions","organizations_url":"https://api.github.com/users/rogernolan/orgs","repos_url":"https://api.github.com/users/rogernolan/repos","events_url":"https://api.github.com/users/rogernolan/events{/privacy}","received_events_url":"https://api.github.com/users/rogernolan/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/Blocked%20On%20Core","name":"Blocked On Core","color":"fad8c7"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/S:P2%20Backlog","name":"S:P2 Backlog","color":"dedede"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/T:Enhancement","name":"T:Enhancement","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":56,"created_at":"2014-11-03T16:29:41Z","updated_at":"2016-10-06T04:20:04Z","closed_at":null,"body":"tl;dr Realm uses thread ids and throws exceptions when they change.  Checking thread ID is not acceptable under GCD.\r\n\r\nRealm docs [state](http://realm.io/docs/cocoa/0.87.1/#background-operations) objects are not gcd queue safe and when running under GCD:\r\n\r\n> you must get an RLMRealm instance in each thread/dispatch_queue in which you want to read or write\r\n\r\nI do this:\r\n\r\n        // Local copy of the persisted task valid for the worker queue.\r\n    __block OFEPersistedTask* taskCopyOnWorkerQueue = nil;\r\n    \r\n    NSString* taskID = aTask.taskID;\r\n    dispatch_sync(_workerQueue, ^{\r\n        NSPredicate *pred = [NSPredicate predicateWithFormat:@\"taskID = %@\", taskID];\r\n        RLMResults *results = [OFEPersistedTask objectsWithPredicate:pred];\r\n        taskCopyOnWorkerQueue = [results firstObject];\r\n    }\r\n\r\nTo get a copy of my Task (RLMObject subclass) on the correct queue. Unfortunately, in `RLMVerifyAttached` Realm checks the thread ID through `RLMCheckThread` (i.e. not queue) which does not work under GCD.\r\n\r\nA GCD queue can run its tasks on many different threads - thats kindof the point. From the [Apple docs](https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html):\r\n\r\n> The currently executing task runs on a distinct thread **(which can vary from task to task)**\r\n\r\nMy **emphasis**... So, even if I grab a RLMObject on my queue I still risk getting killed (actually, no risk, I do get killed) later for running on the wrong thread.\r\n\r\nThe only workaround I can think of is to store a primary key with __block scope and then perform another Realm fetch at the beginning of every task. That feels so suboptimal I don't think you can claim Realm is compatible with GCD _at all_. \r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/251863563","html_url":"https://github.com/realm/realm-cocoa/issues/1097#issuecomment-251863563","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097","id":251863563,"user":{"login":"RashmiZK","id":22638811,"avatar_url":"https://avatars.githubusercontent.com/u/22638811?v=3","gravatar_id":"","url":"https://api.github.com/users/RashmiZK","html_url":"https://github.com/RashmiZK","followers_url":"https://api.github.com/users/RashmiZK/followers","following_url":"https://api.github.com/users/RashmiZK/following{/other_user}","gists_url":"https://api.github.com/users/RashmiZK/gists{/gist_id}","starred_url":"https://api.github.com/users/RashmiZK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RashmiZK/subscriptions","organizations_url":"https://api.github.com/users/RashmiZK/orgs","repos_url":"https://api.github.com/users/RashmiZK/repos","events_url":"https://api.github.com/users/RashmiZK/events{/privacy}","received_events_url":"https://api.github.com/users/RashmiZK/received_events","type":"User","site_admin":false},"created_at":"2016-10-06T04:20:04Z","updated_at":"2016-10-06T04:20:04Z","body":"Thank you for quick response, appreciate it.\n\nWith reference to link - https://github.com/realm/realm-cocoa/issues/2846, I \nwould like to know updates on – fetching Realm objects on background thread \nand passing it to main thread.\n\n\n\nRegards,\n\nRashmi\n\nFrom: JP Simard [mailto:notifications@github.com]\nSent: Thursday, October 06, 2016 4:41 AM\nTo: realm/realm-cocoa\nCc: RashmiZK; Mention\nSubject: Re: [realm/realm-cocoa] Confine on dispatch queue rather than \nthread (#1097)\n\n\n\nThis thread ended up encompassing several different topics, questions and \nscenarios. @RashmiZK <https://github.com/RashmiZK>  can you be more specific \nabout what you'd like clarification or updates on?\n\n—\nYou are receiving this because you were mentioned.\nReply to this email directly, view it on GitHub \n<https://github.com/realm/realm-cocoa/issues/1097#issuecomment-251825814> , \nor mute the thread \n<https://github.com/notifications/unsubscribe-auth/AVlw2ykEpQbmd7D5TW5MRSI6d-6FMYl3ks5qxC6bgaJpZM4C2Vrh> \n.Image removed by sender.\n\n"}}
{"actor":{"display_login":"RashmiZK"}}