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

  • Home
  • SEARCH
  • 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 513267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:24:17+00:00 2026-05-13T07:24:17+00:00

I have multiple views which make the same NSURLRequest/NSURLConnection request . Ideally, in order

  • 0

I have multiple views which make the same NSURLRequest/NSURLConnection request. Ideally, in order to get some code reuse, I’d like to have some sort of a “proxy” which does all the underlying work of creating/executing the (asynchronous) request/connection, setting up all the delegate methods, etc., so I don’t have to copy all those NSURLConnection delegate method handlers in each view. First of all, is this design approach reasonable? Second, how would I go about doing something like that?

For a little background info, I attempted this and got it to “work”, however, it doesn’t appear to be executing asynchronously. I created a Proxy.h/m file which has instance methods for the different web service calls (and also contains the NSURLConnection delegate methods):

@interface Proxy : NSObject {

    NSMutableData *responseData;
    id<WSResponseProtocol> delegate;
}

- (void)searchForSomethingAsync:(NSString *)searchString delegate:(id<WSResponseProtocol>)delegateObj;

@property (nonatomic, retain) NSMutableData *responseData;
@property (assign) id<WSResponseProtocol> delegate;

@end

The WSResponseProtocol is defined as such:

@protocol WSResponseProtocol <NSObject>

@optional
- (void)responseData:(NSData *)data;
- (void)didFailWithError:(NSError *)error;

@end

To use this, the view controller simply needs to conform to the WSResponseProtocol protocol, to catch the response(s). Making the web service call is done like so:

Proxy *p = [[Proxy alloc] init];
[p searchForSomethingAsync:searchText delegate:self];
[p release];

I can provide more code but the remaining can be assumed. Before calling, I “startAnimating” a UIActivityIndicatorView spinner. But the spinner never spins. If I simply put the NSURLConnection delegate methods directly in the view controller, then the spinner spins. So, it makes me think that my implementation isn’t executing asynchronously. Any thoughts/ideas here?

  • 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-13T07:24:18+00:00Added an answer on May 13, 2026 at 7:24 am

    Your approach is reasonable, however, I’m not sure why you are creating your own protocol. This is not necessary. Everything you need to get this implemented is in Apple’s documentation on NSURLConnection. If you take the code from that page where the NSURLConnection is instantiated, and make the connection an ivar instead of just creating it as a local variable, you can then compare connection objects in each of the callback methods and respond accordingly. For example, take this code from the docs and change the connection object to an ivar:

    // create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
    // create the connection with the request
    // and start loading the data
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData that will hold
        // the received data
        // receivedData is declared as a method instance elsewhere
        receivedData=[[NSMutableData data] retain];
    } else {
        // inform the user that the download could not be made
    }
    

    The variable theConnection is our ivar. Then you can check it like this:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        if (connection == theConnection)
        {
            // do something with the data object.
            [connectionSpecificDataObject appendData:data];
        }
    }
    

    You can certainly implement it creating your own protocol as you’re suggesting and then call back out to the delegate that conforms to your protocol, but you may be better off just instantiating your object using a success and failure selector that you can check. Something like this:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        if (connection == theConnection)
        {
            if (delegate && [delegate respondsToSelector:successSelector])
                [delegate performSelector:successSelector 
                               withObject:connectionSpecificDataObject];
        }
        [connection release];
    }
    

    Where dataDidDownloadSelector is a SEL instance variable that you set when you created your download delegate where all of this code is contained–your Proxy object. Something like this:

    Proxy *p = [[Proxy alloc] init];
    [p searchForSomethingAsync:searchText 
                      delegate:self 
               successSelector:@selector(didFinishWithData:) 
                  failSelector:@selector(didFailWithError:)];
    

    Implement your selectors like this:

    - (void)didFinishWithData:(NSData*)data;
    {
        // Do something with data
    }
    
    - (void)didFailWithError:(NSError*)error
    {
        // Do something with error
    }
    

    This has become a longer answer than I intended. Let me know if it doesn’t make sense and I can try to clarify.

    Best regards,

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

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Short answer is it's not specified in the relevant RFC… May 13, 2026 at 2:34 pm
  • Editorial Team
    Editorial Team added an answer For case-insensitive use /I instead of /i. sed -e "/pattern/Id"… May 13, 2026 at 2:34 pm
  • Editorial Team
    Editorial Team added an answer Something in sexylightbox.v2.3.jquery.min.js is causing $ to be set to… May 13, 2026 at 2:34 pm

Related Questions

I'm new to Java portlets, and am trying to get a handle on how
Although I am almost certain the answer to this question will be browser specific,
I have a non document-based Core Data application. There's an NSTreeController that manages a
I have a document-based Core Data app. My main Core Data entity has several
I am looking to use this concept in one of my upcoming project. More

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.