Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS 5 NSURLConnection with NSOperationQueue - Providing UI Feedback
    primarykey
    data
    text
    <p>I need to make multiple NSURLConnections to a JSON Web Service. I would like each WS call to keep in UI informed, probably with a UIActivityIndicatorView and label. So far I've created a NSURLConnection helper class to handle the connection and placed the URL delegates in the View. This works great for updating the UI with a single WS call.</p> <p>For multiple calls, I'm trying to use an NSOperationQueue. I'd like to setMaxConcurrentOperationCount to one on the queue so that each Operation executes one at a time. Here's the relevant code on my View Controller:</p> <p><strong>ViewController.m</strong></p> <pre><code>#import "URLOperationHelper.h" @implementation ViewController - (IBAction)showPopup:(id)sender { // Dictonary holds POST values NSMutableDictionary *reqDic = [NSMutableDictionary dictionary]; // Populate POST key/value pairs [reqDic setObject:@"pw" forKey:@"Password"]; [reqDic setObject:@"ur" forKey:@"UserName"]; operationQueue = [[NSOperationQueue alloc] init]; [operationQueue setMaxConcurrentOperationCount:1]; [operationQueue cancelAllOperations]; [operationQueue setSuspended:YES]; URLOperationHelper *wsCall1 = [[URLOperationHelper alloc] initWithURL:@"urlString1" postParameters:reqDic urlDelegate:self]; URLOperationHelper *wsCall2 = [[URLOperationHelper alloc] initWithURL:@"urlString2" postParameters:reqDic urlDelegate:self]; [operationQueue addOperation:wsCall1]; [operationQueue addOperation:wsCall2]; } // Did the URL Connection receive a response -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"Did receive response: %@", response); NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; int code = [httpResponse statusCode]; // Handle status code here webData = [[NSMutableData alloc]init]; } // Did the URL Connection receive data -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"Did receive data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); assert(webData != nil); [webData appendData:data]; } // Did the connection fail with an error? -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@", error); } // Executes after a successful connection and data download -(void) connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Connection finished"); } @end </code></pre> <p>And here is my <strong>URLOperationHelper.m</strong></p> <pre><code> @implementation URLHelper - (id)initWithURL:(NSString *)urlPath postParameters:(NSMutableDictionary *)postParameters urlParentDelegate:(id) pDelegate { if(self = [super init]) { connectionURL = urlPath; postParams = postParameters; parentDelegate = pDelegate; } return self; } - (void)done { // Cancel the connection if present if(urlConnection) { [urlConnection cancel]; urlConnection = nil; } // Alert [self willChangeValueForKey:@"isExecuting"]; [self willChangeValueForKey:@"isFinished"]; executing = NO; finished = YES; [self willChangeValueForKey:@"isFinished"]; [self willChangeValueForKey:@"isExecuting"]; } - (void)cancel { // Possibly add an NSError Property [self done]; } - (void)start { // Make sure this operation starts on the main thread if(![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO]; return; } // Make sure that the operation executes if(finished || [self isCancelled]) { [self done]; return; } [self willChangeValueForKey:@"isExecuting"]; executing = YES; [self main]; [self willChangeValueForKey:@"isExecuting"]; } - (void)main { NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postParams options:NSJSONWritingPrettyPrinted error:&amp;error]; // Convert dictionary to JSON NSString *requestJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"JSONRequest: %@", requestJSON); // Declare Webservice URL, request, and return data url = [[NSURL alloc] initWithString:connectionURL]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSData *requestData = [NSData dataWithBytes:[requestJSON UTF8String] length:[requestJSON length]]; // Build the request [request setHTTPMethod:@"POST"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:requestData]; // Connect to Webservice // Responses are handled in the delegates below urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:parentDelegate startImmediately:YES]; } - (BOOL)isConcurrent { return YES; } - (BOOL)isExecuting { return executing; } -(BOOL)isFinished { return finished; } @end </code></pre> <p>The problem that I'm having is the Start method for the URLOperation is never called. The OperationQueue is created and the Operations are called, but nothing happens after that, execution or thread wise. </p> <p>Also, is this a correct line of thinking to provide UI feedback using NSOperationQueues like this? I.E. calling the NSURLDelegates from the Operation?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload