{"action":"created","issue":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/566","labels_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/566/labels{/name}","comments_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/566/comments","events_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/566/events","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/566","id":19153477,"number":566,"title":"MR_contextForCurrentThread used of ThreadContext is problematic","user":{"login":"carlshimer","id":247782,"avatar_url":"https://avatars.githubusercontent.com/u/247782?v=3","gravatar_id":"","url":"https://api.github.com/users/carlshimer","html_url":"https://github.com/carlshimer","followers_url":"https://api.github.com/users/carlshimer/followers","following_url":"https://api.github.com/users/carlshimer/following{/other_user}","gists_url":"https://api.github.com/users/carlshimer/gists{/gist_id}","starred_url":"https://api.github.com/users/carlshimer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlshimer/subscriptions","organizations_url":"https://api.github.com/users/carlshimer/orgs","repos_url":"https://api.github.com/users/carlshimer/repos","events_url":"https://api.github.com/users/carlshimer/events{/privacy}","received_events_url":"https://api.github.com/users/carlshimer/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2013-09-07T23:45:58Z","updated_at":"2015-12-17T09:34:49Z","closed_at":"2013-12-29T14:30:03Z","body":"General problem:\r\n\r\nOur application supports the ability for the user to log out and log in again with a different user name / password.  We were seeing strange issues and /or crashes with core Data after a user logged out and logged in again.\r\n\r\nHere is what we are doing under the hood:\r\n1) When user clicks logout, we shutdown all our operation queues, clean up Magical record using the cleanup method, and finally delete the sqlite database.\r\n2) User logs in, we set up core data / magical record from scratch.\r\n3) Download data from the server.\r\n\r\nWhat I noticed is that was that on some occasions it looked like we were getting data from the old database despite the fact that it had been deleted and Magical record had been cleaned up / shutdown.  I finally tracked down the problem to MR_contextForCurrentThread returning a NSManagedObjectContext from the ThreadContext that was created with the old database.  Said another way, despite shutting down my operation queues as cleanly as possible the system is still reusing threads and the thread local storage associated with them.  \r\n\r\nI figured out that what I really need was to iterate through all of my threads and call MR_resetContextForCurrentThread.  I honestly don't know of an easy way to do that - you can iterate threads at the C level using mach based APIs but not sure you can get access to the ThreadContext.  Another possibility is to broadcast a message to all threads using notifications but this seems problematic as well since you don't have control over which thread runs the notification handler.\r\n\r\nThe fundamental problem seems to be that OperationQueue's use threads but want to hide the details from you while CoreData absolutely requires you to be aware of thread management.\r\n\r\nMy current solution is to use a global runcount unsigned integer that I increment when the user logs out.  I modified MR_contextForCurrentThread to check for this global variable (via the AppDelegate) and reset the thread local MOC.  So far this has fixed my problems.\r\n\r\nHas anyone run into this problem or can offer an alternative solution? I can make this into a pull request if anyone is interested...\r\n\r\n\r\n\r\n\r\n \r\n"},"comment":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/comments/165396164","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/566#issuecomment-165396164","issue_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/566","id":165396164,"user":{"login":"grittymindy","id":11732306,"avatar_url":"https://avatars.githubusercontent.com/u/11732306?v=3","gravatar_id":"","url":"https://api.github.com/users/grittymindy","html_url":"https://github.com/grittymindy","followers_url":"https://api.github.com/users/grittymindy/followers","following_url":"https://api.github.com/users/grittymindy/following{/other_user}","gists_url":"https://api.github.com/users/grittymindy/gists{/gist_id}","starred_url":"https://api.github.com/users/grittymindy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grittymindy/subscriptions","organizations_url":"https://api.github.com/users/grittymindy/orgs","repos_url":"https://api.github.com/users/grittymindy/repos","events_url":"https://api.github.com/users/grittymindy/events{/privacy}","received_events_url":"https://api.github.com/users/grittymindy/received_events","type":"User","site_admin":false},"created_at":"2015-12-17T09:34:48Z","updated_at":"2015-12-17T09:34:48Z","body":"I've read this issue description and all the comments, also the [post](http://saulmora.com/coredata/magicalrecord/2013/09/15/why-contextforcurrentthread-doesn-t-work-in-magicalrecord.html) by @casademora. Still don't know why a reused thread and the cached MOC in thread context will cause the crash. The MOC is not used by a different thread, but the same one, right?  Can you explain that in detail, please? @carlshimer @casademora @daveanderson Thanks a lot!"}}