2

I want to upload some files which should continue even if the application goes into background.

Currently I am retrieving the files from the DB and than adding it to the Queue via NSOperation which is then starts the upload procedure.

All the files should be uploaded even if the app goes to background or foreground. Below is the code for single task can anyone give me a hint how we can make it to work for uploading many files.

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  // Clean up any unfinished task business by marking where you
  // stopped or ending the task outright.

  [application endBackgroundTask: bgTask];
  bgTask = UIBackgroundTaskInvalid;
}];
nikhil84
  • 3,235
  • 4
  • 22
  • 43
Ekra
  • 3,241
  • 10
  • 41
  • 61

2 Answers2

0

Apple allows background execution if your app follows and satisfy the necessary condition. plese refer here

Community
  • 1
  • 1
user968597
  • 1,164
  • 2
  • 15
  • 30
0

Why not try doing this? .. I haven't yet tried it out, I'll post an update after attempting this

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  // Clean up any unfinished task business by marking where you
  // stopped or ending the task outright.

  // Bring up your NSOperation queue instance here and block this thread until it is complete
  [queue waitUntilAllOperationsAreFinished];

  [application endBackgroundTask: bgTask];
  bgTask = UIBackgroundTaskInvalid;
}]; 

also ensure that you have a way to cancel all these long standing operation in the background

 bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [queue cancelAllOperations];

        [application endBackgroundTask:endSessionTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
Santthosh
  • 1,083
  • 1
  • 12
  • 30
  • 1
    I don't think this answer is right. The `waitUntilAllOperationsAreFinished` call should be made after the `beginBackgroundTaskWithExpirationHandler`, not inside the expiration block. See this answer: http://stackoverflow.com/questions/10319643/objective-c-proper-use-of-beginbackgroundtaskwithexpirationhandler – Dima Dec 08 '14 at 22:32