{"id":26429,"date":"2019-08-29T15:23:36","date_gmt":"2019-08-29T19:23:36","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26429"},"modified":"2019-08-29T15:23:36","modified_gmt":"2019-08-29T19:23:36","slug":"the-curious-case-of-the-core-data-crash","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/08\/29\/the-curious-case-of-the-core-data-crash\/","title":{"rendered":"The Curious Case of the Core Data Crash"},"content":{"rendered":"<p><a href=\"https:\/\/blog.iconfactory.com\/2019\/08\/the-curious-case-of-the-core-data-crash\/\">Sean Heber<\/a>:<\/p>\n<blockquote cite=\"https:\/\/blog.iconfactory.com\/2019\/08\/the-curious-case-of-the-core-data-crash\/\">\n<p>iOS routinely terminates apps for all sorts of reasons, including using too much memory or taking too long to do background processing. I don&rsquo;t know why this information isn&rsquo;t included in Xcode&rsquo;s Organizer, but it&rsquo;s a critical piece of a debugging puzzle.<\/p>\n<p>[&#8230;]<\/p>\n<p>But one result let to <a href=\"https:\/\/developer.apple.com\/library\/archive\/technotes\/tn2151\/_index.html\">Tech Note 2151<\/a>, with the code listed at the end under Other Exception Types:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/library\/archive\/technotes\/tn2151\/_index.html\"><p>The exception code 0xdead10cc indicates that an application has been terminated by the OS because it held on to a file lock or sqlite database lock during suspension.<\/p>\n<\/blockquote><p>[&#8230;]<\/p>\n<p>I discovered that Twitterrific was sometimes closed while it was doing a network download and iOS left it running in the background long enough for the network request to finish. But not long enough for the database update to finish.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/BigZaphod\/status\/1164977093442191360\">Sean Heber<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/BigZaphod\/status\/1164977093442191360\"><p>Converting to <code>NSURLSession<\/code> wasn&rsquo;t very hard and we did it a long time ago. However something had been bothering me and I could never quite pin it down. Specifically, it really felt like network tasks that started while foregrounded and ended while backgrounded were less reliable.<\/p><p>[&#8230;]<\/p><p>I eventually determined that sometimes a request would fail to start and the error code returned by the <code>NSURLSessionDataTask<\/code> would be something odd like <code>NSURLErrorNetworkConnectionLost<\/code> or the ever-helpful <code>NSURLErrorUnknown<\/code>.<\/p><p>[&#8230;]<\/p><p>I scoured the <code>NSURLSession<\/code>s docs looking for a clue and came across something I had missed all the times before - a property called <code>shouldUseExtendedBackgroundIdleMode<\/code> &ldquo;In addition to requesting that the connection be kept open &#8230; when the app moves to the background.&rdquo;<\/p><p>Waitaminute! What is this? Does this mean there&rsquo;s now an assumption that the connection will NOT be kept open when moving into the background?! Holy buckets on a wagon.<\/p><p>It appears that they REALLY want you to use the special background support in <code>NSURLSession<\/code>. In fact they seem to say to go ahead and use it all the time - don&rsquo;t try to make it conditional.<\/p><p>[&#8230;]<\/p><p>And just like that, the flakiness seemed to be gone. For years I made the assumption that an open <code>NSURLSession<\/code> connection would be allowed to finish as long as you had a background task active like <code>NSURLConnection<\/code> did - but NOPE it doesn&rsquo;t do that by default!<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Sean Heber: iOS routinely terminates apps for all sorts of reasons, including using too much memory or taking too long to do background processing. I don&rsquo;t know why this information isn&rsquo;t included in Xcode&rsquo;s Organizer, but it&rsquo;s a critical piece of a debugging puzzle. [&#8230;] But one result let to Tech Note 2151, with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-08-29T19:23:39Z","apple_news_api_id":"c045cee8-8939-4b81-a0fa-702433916370","apple_news_api_modified_at":"2019-08-29T19:23:40Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AwEXO6Ik5S4Gg-nAkM5FjcA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,109,31,1610,772,476,71,425,233],"class_list":["post-26429","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-coredata","tag-ios","tag-ios-12","tag-ios-multitasking","tag-networking","tag-programming","tag-sqlite","tag-twitterrific"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=26429"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26429\/revisions"}],"predecessor-version":[{"id":26430,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26429\/revisions\/26430"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}