Hi,
I'm using a NSURLSession with the backgroundSessionConfiguration to send a NSURLSessionUploadTask in the background.
I noticed that sometimes after waking by the OS in the app delegate's handleEventsForBackgroundURLSession, I get a call on the NSURLSessionDataDelegate for URLSession:dataTask:didReceiveData *directly before* URLSession:task:didCompleteWithError. They seem to be called by two different threads in the same time. Naturally, processing the didComplete before getting the data in didReceive results in bugs. Is this the normal behavior for background tasks and I should support these parallel out of order calls to the delegate for the same task, or is it a bug and just open a rdar?
Thanks
The problem here relates to how you construct
networkOperationQueue
. By default NSOperationQueue will return a concurrent queue, that is, a queue on which multiple operations can run concurrently. If you want the operations to be serialised, which is the
only sane way to deal with NSURLSession callbacks, set the queue’s
maxConcurrentOperationCount
to 1.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"