{"action":"created","issue":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1219","repository_url":"https://api.github.com/repos/magicalpanda/MagicalRecord","labels_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1219/labels{/name}","comments_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1219/comments","events_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1219/events","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1219","id":146986470,"number":1219,"title":"MR_saveToPersistentStoreAndWait unsafe?","user":{"login":"nicklocking","id":791449,"avatar_url":"https://avatars.githubusercontent.com/u/791449?v=3","gravatar_id":"","url":"https://api.github.com/users/nicklocking","html_url":"https://github.com/nicklocking","followers_url":"https://api.github.com/users/nicklocking/followers","following_url":"https://api.github.com/users/nicklocking/following{/other_user}","gists_url":"https://api.github.com/users/nicklocking/gists{/gist_id}","starred_url":"https://api.github.com/users/nicklocking/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nicklocking/subscriptions","organizations_url":"https://api.github.com/users/nicklocking/orgs","repos_url":"https://api.github.com/users/nicklocking/repos","events_url":"https://api.github.com/users/nicklocking/events{/privacy}","received_events_url":"https://api.github.com/users/nicklocking/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-08T17:14:41Z","updated_at":"2016-04-23T15:44:12Z","closed_at":null,"body":"In my project (Whisper), we recently implemented CoreData/MagicalRecord as a data model to replace the old one, which was not good. The old implementation was CoreData, but everything used only a single context, performing all operations on the main thread, which is obviously quite bad.\r\n\r\nThe app contains quite a lot of CoreData operations, so we released a version of the app with a sort of hybrid model, where the most important stuff was done with MagicalRecord. The general pattern was: for processing responses from our API, we create an NSOperation that contained a single MagicalRecord.saveWithBlockAndWait call. The operation queue is serial (maxConcurrentOperationCount = 1) to avoid object duplication issues. This is a pattern I've used before and like a lot.\r\n\r\nThe stuff we didn't migrate over tended to have a simple yet crap pattern - it would make some changes on the default context and then do MR_saveToPersistentStoreAndWait (on the main thread) to ensure they were written to disk. We considered this not a huge problem (although definitely tech debt) because it was generally operations that didn't happen very often.\r\n\r\nWhat we experienced when we did this was a massive amount of mysterious CoreData errors. \"Statement is still active\", EXC_BAD_ACCESS (referring to the context), and similar. Usually they manifested when the rootSavingContext merged in changes from a background thread. We found reproducing these issues in-house to be next to impossible, and they never revealed anything useful the few times they did occur locally.\r\n\r\nWe banged our heads against this for a week or so - see this unanswered StackOverflow question while listening to the saddest Cure songs you can find for an approximate representation of the truly bleak emotional experience I had checking it the day after I created it:\r\n\r\nhttp://stackoverflow.com/questions/36144179/coredatamagicalrecord-nsinternalconsistencyexception-statement-is-still-active\r\n\r\nWe just released a version that contains absolutely no MR_saveToPersistentStoreAndWait calls, because it seemed to us like the default context save and the background context saves were conflicting in some way. This completely (or close enough to it that we don't care very much) fixes the problem. \r\n\r\nAs a result we are now operating on a forked version of MR 2.3.2 that deprecates MR_saveToPersistentStoreAndWait, because we consider it unsafe, or at least, unsafe from the main thread. Are we wrong?"},"comment":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/comments/213767485","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1219#issuecomment-213767485","issue_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1219","id":213767485,"user":{"login":"francosolerio","id":244726,"avatar_url":"https://avatars.githubusercontent.com/u/244726?v=3","gravatar_id":"","url":"https://api.github.com/users/francosolerio","html_url":"https://github.com/francosolerio","followers_url":"https://api.github.com/users/francosolerio/followers","following_url":"https://api.github.com/users/francosolerio/following{/other_user}","gists_url":"https://api.github.com/users/francosolerio/gists{/gist_id}","starred_url":"https://api.github.com/users/francosolerio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/francosolerio/subscriptions","organizations_url":"https://api.github.com/users/francosolerio/orgs","repos_url":"https://api.github.com/users/francosolerio/repos","events_url":"https://api.github.com/users/francosolerio/events{/privacy}","received_events_url":"https://api.github.com/users/francosolerio/received_events","type":"User","site_admin":false},"created_at":"2016-04-23T15:44:12Z","updated_at":"2016-04-23T15:44:12Z","body":"I've been banging my head against this bug for some months with zero results."}}