I have a Requirement where I need to Create and Start a thread when application enters the Background State. The function of the Created Thread is to fetch data from local DB and upload to server, and I don't need to do any updates on UI. My questions are:
- Where exactly should I create the Thread - either in
applicationWillResignActive
method or inapplicationDidEnterBackground
? - Which is best way of creating the Thread - nsthread way or GCD or Posix Way?
The things I've tried
- I've already worked on Android so I know how can I achieve this scenario, but I'm new to iOS, so I'm getting bit confused to start with.
I just tried the with NSThread concept, but it was not working. Below is code that I wrote to the best of my knowledge:
- (void)applicationWillResignActive:(UIApplication *)application { [NSThread detachNewThreadSelector:@selector(FetchReportFromDBAndUpload) toTarget:self withObject:nil]; } - (void)FetchReportFromDBAndUpload { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //Check for internet connection and fetch data from DB and upload to server [pool release]; }
Is this correct way of creating the thread, or do I need to do some changes? If not please guide me how to achieve this. Thanks in advance