1

In my app I am creating a background session with a single identifier, (using the singleton method) that has ~50-100 upload tasks(images) when the user tries to upload their "Orders". Each Order has ~25 images to upload.

I am storing the OrderID as the taskDescription and my delegate method looks like this

-(void)URLSession:(NSURLSession *)session
         task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
    if(!error)
    {
        [session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks,     NSArray *downloadTasks) 
        {
            bool hasMoreTasks = false;

            //Loops through tasks, if there is another task with same identifier
            //then DON'T change status of order to uploaded
            for(NSURLSessionUploadTask *uTask in uploadTasks)
            {
                if([uTask.taskDescription isEqualToString:task.taskDescription])
                {
                    hasMoreTasks = true;
                    break;
                }
            }

            if(!hasMoreTasks) {
                NSLog(@"all tasks completed for order: %@", task.taskDescription);
                //Get Order from Core Data, update Order status to "Sent"
                dispatch_async(dispatch_get_main_queue(), ^{
                     //Stop the spinner on my page and decrement number of orders ready
                     //for upload
                });
            }
       }
    }

}

All orders upload just fine to my server. its updating the UI and CoreData that is the problem. If I queue 3 orders for upload, it seems all 3 upload but in the end it says that one order still isn't uploaded.

Just for debugging purposes here is my viewWillAppear

-(void)viewWillAppear
{
     NSLog(@"viewWillAppear");
     [self.backgroundSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
        NSLog(@"task count: %@", [NSNumber numberWithInteger:[uploadTasks count]]);
        for(NSURLSessionUploadTask *task in uploadTasks)
        {
            NSLog(@"upload task %ld", task.state);

        }
     }];
}

At first when using this app the log will show "viewWillAppear", then "Task Count" and all the upload tasks, but after i try to upload or use the app a bit more its like the thread my background session is on stops working entirely and the only thing that will be logged is "viewWillAppear" no matter how long I wait.

It seems like something is blocking my background thread/queue. Sometimes I will also have tasks in the running state that aren't doing anything..

Any help/suggestions is much appreciated.

0 Answers0