0

I want to send a timestamp to a remote server, and wait for the callback of success, then store the timestamp locally, if remote server did not respond.

Is it something that I could put into applicationDidEnterBackground implementation?

JAL
  • 41,701
  • 23
  • 172
  • 300
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

3 Answers3

6

According to iOS Developer Library UIApplicationDelegate Protocol Reference

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

So you have approximately 5 seconds to perform any tasks and return in "applicationDidEnterBackground" methods.

ChihHao
  • 273
  • 1
  • 4
  • 15
1

If you want to do any network stuff before going to the background, you should ask for extra time with beginBackgroundTaskWithExpirationHandler:.

nevan king
  • 112,709
  • 45
  • 203
  • 241
0

You can do that in dispatch_async and you can wait the server answer and do something with your data, but you can't stop the enter background...

try this code

dispatch_queue_t savingQueue = dispatch_queue_create("savingQue", NULL);
dispatch_async(savingQueue, ^{

   //do something in bg.... 

    dispatch_async(dispatch_get_main_queue(), ^{


    });

});
dispatch_release(savingQueue);
Imodeveloper
  • 394
  • 4
  • 13