Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:29:59+00:00 2026-05-31T03:29:59+00:00

I need to make multiple NSURLConnections to a JSON Web Service. I would like

  • 0

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.

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:

ViewController.m

#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 

And here is my URLOperationHelper.m

  @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:&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

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.

Also, is this a correct line of thinking to provide UI feedback using NSOperationQueues like this? I.E. calling the NSURLDelegates from the Operation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:30:00+00:00Added an answer on May 31, 2026 at 3:30 am

    If you set setSuspended to YES before adding the Operations then your Operations will be queued into a suspended queue.. i suggest not to suspend the queue at

    furthermore, your operation never ends anyway. You need to assign the operation itself as the delegate and implement all necessary delegate methods. In these methods you can forward the messages to your parentDelegate and decide when you are finished and call your done method when appropriate (i suggest connection:didFailWithError: and connectionDidFinishLoading:)

    There is a good tutorial here: http://blog.9mmedia.com/?p=549

    You are also not completely implementing key-value-coding compilant properties correct. Whenever you call willChangeValueForKey: you also need to call didChangeValueForKey afterwards:

    - (void)start
    {
        ...
        [self willChangeValueForKey:@"isExecuting"];
        executing = YES;
        [self didChangeValueForKey:@"isExecuting"];
    
        [self main];
    }
    

    and:

    - (void)done
    {
        ...
    
        // Alert
        [self willChangeValueForKey:@"isExecuting"];
        [self willChangeValueForKey:@"isFinished"];
    
        executing = NO;
        finished = YES;
    
        [self didChangeValueForKey:@"isFinished"];
        [self didChangeValueForKey:@"isExecuting"];
    }
    

    See this Q/A for KVC: when to use "willChangeValueForKey" and "didChangeValueForKey"?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a change to an ASP.NET web service written a couple
I have a method where it need to make multiple service calls and consolidate
I need to make multiple asynchronous service calls in the application:didFinishLaunchingWithOptions: method from my
I need to make multiple asynchronous calls from inside a wcf service hosted in
I have a multiple select dropdown box. I need to make an ajax call
I need to make multiple divs move from right to left across the screen
I need to make a ListAdapter that presents data from multiple ContentProviders. The ContentProviders
I'm trying to make the code below reusable. I need multiple toggle buttons in
I need to make a custom ListView with multiple view types. I found this
So my objective sounds simple, I need to make a db driven multiple choice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.