0

I was looking into a debug file my application creates while running and saw a strange behavior: The application uses NSURLConnction to retrieve some data from a server. The connection was started and then the user moved application to the background probable by pressing the home button (application:willResignActive and application:didEnterBackground methods were called). I could see that the connection was handled in my code after application:didEnterBackground was called. (The connectionDidFinishLoading of NSURLConnection delegate was called after application:didEnterBackground). In the implementation of connectionDidFinishLoading I perform some operations, but at this point application was probable suspended and the code wasn't called. Then after almost 12 hours user launched application again (it wasn't terminated, but only was in the background) and the code continued running from the same point it stopped, even before application:willEnterForeground and application:didBecomeActive were called). Does it make scenes? The part of code running before application entered to the foreground changed my application internal state and cause to strange behavior.

Any ideas how to solve it?

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62

1 Answers1

0

after you receive application:didEnterBackground you have about 5 seconds to do the finishing stuff, after which your application is no longer being scheduled on the cpu.

In your case, the best solution is to announce the start of background work via beginBackgroundTaskWithExpirationHandler: and finish the download and related tasks.

Farcaller
  • 3,070
  • 1
  • 27
  • 42
  • Thanks for your answer. The connection is very short, and ended before the 5 seconds limitation of application:didEnterBackground. I can see in the debug file that that didEnterBackground is called and then after few miliseconds the connection was ended (didFinishLoading was called) – user1513930 Aug 14 '12 at 04:55