Question

I have an HTTPClient request as follows :

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:urlStringMain]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        //parameters
                        nil];

[self beginBackgroundUpdateTask];

[httpClient postPath:postPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

    //id results = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONWritingPrettyPrinted error:nil];

    //completion code

    [self endBackgroundUpdateTask];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    //failure code

}];
[httpClient release];

The background task is executed in :

- (void) beginBackgroundUpdateTask{

[operationQueue addOperationWithBlock:^{

    NSLog(@"started upload process as a background job");

    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}];

}

And ended in :

- (void) endBackgroundUpdateTask{

NSLog(@"complete upload process as a background job");

[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}

where self.backgroundUpdateTask is a UIBackgroundTaskIdentifier object, and operationQueue is an object of NSOperationQueue (public member), initialized in viewDidLoad:

operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];

Now what I want to do is, perform these requests chronologically, in the background, such that pushing/popping from the viewController does not affect the request. It should also not be affected if the application goes into background. At times I post text, and at other times I post an image. Now, images take longer to upload, than text so if subsequent requests of text and images are made, texts are posted first, and images later. This breaks the chronology of the tasks, hence I wanted to use the NSOperationQueue. But being new to operation queues, I cannot seem to make it work. The chronology is still not being respected. How do I perform the task in the way I want to.

PS. Also, as you can see in the code, i have added [self endBackgroundUpdateTask] in both, the completion block of the httpClient request, and the beginBackgroundUpdateTask method. Now i understand this is not good. Where exactly should the endBackgroundUpdateTask method be called ?

Thank you.

Was it helpful?

Solution

The HTTPClient can handle the background task for you if you ask it to, simply set the appropriate flag (you need to create the operation and call setShouldExecuteAsBackgroundTaskWithExpirationHandler:).

Rather than change how the uploads operate, keep it simple. Send a date with the upload and use that to maintain your order information on the server.


If you really must execute your operations serially then you can get the operationQueue from the client and set it to only execute a single operation at a time.

In either case you shouldn't need to create your own operation queue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top