The Curious Case of the Core Data Crash
iOS routinely terminates apps for all sorts of reasons, including using too much memory or taking too long to do background processing. I don’t know why this information isn’t included in Xcode’s Organizer, but it’s a critical piece of a debugging puzzle.
[…]
But one result let to Tech Note 2151, with the code listed at the end under Other Exception Types:
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.
[…]
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.
Converting to
NSURLSession
wasn’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.[…]
I eventually determined that sometimes a request would fail to start and the error code returned by the
NSURLSessionDataTask
would be something odd likeNSURLErrorNetworkConnectionLost
or the ever-helpfulNSURLErrorUnknown
.[…]
I scoured the
NSURLSession
s docs looking for a clue and came across something I had missed all the times before - a property calledshouldUseExtendedBackgroundIdleMode
“In addition to requesting that the connection be kept open … when the app moves to the background.”Waitaminute! What is this? Does this mean there’s now an assumption that the connection will NOT be kept open when moving into the background?! Holy buckets on a wagon.
It appears that they REALLY want you to use the special background support in
NSURLSession
. In fact they seem to say to go ahead and use it all the time - don’t try to make it conditional.[…]
And just like that, the flakiness seemed to be gone. For years I made the assumption that an open
NSURLSession
connection would be allowed to finish as long as you had a background task active likeNSURLConnection
did - but NOPE it doesn’t do that by default!