{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097","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":"Support object reuse across gcd tasks","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/Enhancement","name":"Enhancement","color":"207de5"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/P2","name":"P2","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":40,"created_at":"2014-11-03T16:29:41Z","updated_at":"2015-09-25T06:21:27Z","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/143140106","html_url":"https://github.com/realm/realm-cocoa/issues/1097#issuecomment-143140106","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1097","id":143140106,"user":{"login":"Kirow","id":1594953,"avatar_url":"https://avatars.githubusercontent.com/u/1594953?v=3","gravatar_id":"","url":"https://api.github.com/users/Kirow","html_url":"https://github.com/Kirow","followers_url":"https://api.github.com/users/Kirow/followers","following_url":"https://api.github.com/users/Kirow/following{/other_user}","gists_url":"https://api.github.com/users/Kirow/gists{/gist_id}","starred_url":"https://api.github.com/users/Kirow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kirow/subscriptions","organizations_url":"https://api.github.com/users/Kirow/orgs","repos_url":"https://api.github.com/users/Kirow/repos","events_url":"https://api.github.com/users/Kirow/events{/privacy}","received_events_url":"https://api.github.com/users/Kirow/received_events","type":"User","site_admin":false},"created_at":"2015-09-25T06:21:27Z","updated_at":"2015-09-25T06:21:27Z","body":"@ReDetection if you will execute queries using GCD - this will not guarantee single thread. If you will use nested NSThread for this task - this will be much more slower. So I think GCD serial queue support is highly required. In fact if I remove exception from realm code, it works fine, but I were told not to do that."}}