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 6243273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:06:32+00:00 2026-05-24T12:06:32+00:00

I’m creating an app which communicates alot with a server. I want to make

  • 0

I’m creating an app which communicates alot with a server. I want to make sure I have only 1 connection at a time – only 1 request pending. I want so that if I try to send another request, It will wait until the current one is finished before sending the next.

How can I implement This?
Tnx!

  • 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-24T12:06:33+00:00Added an answer on May 24, 2026 at 12:06 pm

    I don’t know of any automatic mechanism to do this. So you have to write a new class yourself (let’s call it ConnectionQueue):

    Basically, instead of a creating the NSURLConnection directly, you call a method of your ConnectionQueue class (which should have exactly one instance) taking the NSURLRequest and the delegate as a parameter. Both are added to a queue, i.e. a separate NSArray for the requests and the delegates.

    If the arrays contains just one element, then no request is outstanding and you can create a NSURLConnection with the specified request. However, instead of the delegate passed to the method, you pass your ConnectionQueue instance as the delegate. As a result, the connection queue will be informed about all actions of the connection. In most cases, you just forward the callback to the original delegate (you’ll find it in the first element of the delegate array).

    However, if the outstanding connection completes (connection:didFailWithError: or connectionDidFinishLoading: is called), you first call the original delegate and then remove the connection from the two arrays. Finally, if the arrays aren’t empty, you start the next connection.

    Update:

    Here’s some code. It compiles but hasn’t been tested otherwise. Furthermore, the implementation of the NSURLConnectionDelegate protocol is incomplete. If you’re expecting more than implemented callbacks, you’ll have to add them.

    Header File:

    #import <Foundation/Foundation.h>
    
    @interface ConnectionQueue : NSObject {
        NSMutableArray *requestQueue;
        NSMutableArray *delegateQueue;
        NSURLConnection *currentConnection;
    }
    
    // Singleton instance
    + (ConnectionQueue *)sharedInstance;
    
    // Cleanup and release queue
    + (void)releaseShared;
    
    // Queue a new connection
    - (void)queueRequest:(NSURLRequest *)request delegate:(id)delegate;
    
    @end
    

    Implementation:

    #import "ConnectionQueue.h"
    
    
    @implementation ConnectionQueue
    
    
    static ConnectionQueue *sharedInstance = nil;
    
    
    + (ConnectionQueue*)sharedInstance
    {
        if (sharedInstance == nil)
            sharedInstance = [[ConnectionQueue alloc] init];
        return sharedInstance;
    }
    
    + (void)releaseShared
    {
        [sharedInstance release];
        sharedInstance = nil;
    }
    
    - (id)init
    {
        if ((self = [super init])) {
            requestQueue = [NSMutableArray arrayWithCapacity:8];
            delegateQueue = [NSMutableArray arrayWithCapacity:8];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [requestQueue release];
        [delegateQueue release];
        [currentConnection cancel];
        [currentConnection release];
        [super dealloc];
    }
    
    - (void)startNextConnection
    {
        NSURLRequest *request = [requestQueue objectAtIndex:0];
        currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    
    - (void)queueRequest:(NSURLRequest *)request delegate:(id)delegate
    {
        [requestQueue addObject:request];
        [delegateQueue addObject:delegate];
        if ([requestQueue count] == 1)
            [self startNextConnection];
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        id delegate = [delegateQueue objectAtIndex:0];
        [delegate connection: connection didReceiveResponse: response];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        id delegate = [delegateQueue objectAtIndex:0];
        [delegate connection: connection didReceiveData: data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        id delegate = [delegateQueue objectAtIndex:0];
        [delegate connection: connection didFailWithError:error];
        [currentConnection release];
        currentConnection = nil;
        [requestQueue removeObjectAtIndex:0];
        [delegateQueue removeObjectAtIndex:0];
    
        if ([requestQueue count] >= 1)
            [self startNextConnection];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        id delegate = [delegateQueue objectAtIndex:0];
        [delegate connectionDidFinishLoading: connection];
        [currentConnection release];
        currentConnection = nil;
        [requestQueue removeObjectAtIndex:0];
        [delegateQueue removeObjectAtIndex:0];
    
        if ([requestQueue count] >= 1)
            [self startNextConnection];
    }
    
    @end
    

    To use the connection queue, create an NSURLRequest instance and then call:

    [[ConnectionQueue sharedInstance] queueRequest:request delegate:self];
    

    There’s no need to create the singleton instance of ConnectionQueue explicitly. It will be created automatically. However, to properly clean up, you should call [ConnectionQueue releaseShared] when the application quits, e.g. from applicationWillTerminate: of your application delegate.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.