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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:35:38+00:00 2026-05-30T05:35:38+00:00

I need to make some POST calls to my server, but I need to

  • 0

I need to make some POST calls to my server, but I need to not block the main thread. As I understand, NSMutableURLRequest and NSURLConnection are not thread safe, so it is best to use the async method of NSURLConnection.

My question about this is, how I can package it up nicely into a method, instead of having to use the delegate method? I would prefer to do:

NSData *returnedData = [Utility postDataToURL:@"some string of data"];

This is how it is easy done with the following method:

[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];

It is so nice keeping everything within one method, then just having my data returned from it!

Are there any block based methods for this? It becomes an issue when I need to write methods for about 50 different calls and each one needs to use the same delegate method. Am I going about this the wrong way?

This will only need to be for iOS5.

  • 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-30T05:35:40+00:00Added an answer on May 30, 2026 at 5:35 am

    iOS 5 adds sendAsynchronousRequest:queue:completionHandler: which does what I think you want. I’ve got my code set up to use that if available, but to fall back on performing a synchronous fetch on a background GCD queue and hopping onto the main thread with the result if it doesn’t. The latter will be less power efficient but it’s just to maintain legacy support.

    if([NSURLConnection respondsToSelector:@selector(sendAsynchronousRequest:queue:completionHandler:)])
    {
        // we can use the iOS 5 path, so issue the asynchronous request
        // and then just do whatever we want to do
        [NSURLConnection sendAsynchronousRequest:request
            queue:[NSOperationQueue mainQueue]
            completionHandler:
            ^(NSURLResponse *response, NSData *data, NSError *error)
            {
                [self didLoadData:data];
            }];
    }
    else
    {
        // fine, we'll have to do a power inefficient iOS 4 implementation;
        // hop onto the global dispatch queue...
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
        ^{
            // ... perform a blocking, synchronous URL retrieval ...
            NSError *error = nil;
            NSURLResponse *urlResponse = nil;
            NSData *responseData =
                [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
            // ... and hop back onto the main queue to handle the result
            dispatch_async(dispatch_get_main_queue(),
            ^{
                [self didLoadData:responseData];
            });
        });
    }
    

    In production code you’d actually check the errors and HTTP response codes (as a server 404 response is probably just as much an error from your point of view as a connection failure), obviously.

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

Sidebar

Related Questions

I need to download file from FTP server and make some changes on it
I need to implement a way to make POST calls to pages located on
I need to make some changes to an old Oracle stored procedure on an
I need to make some recurrent modification in a lot of files (C++). I
I need to make some part of text bold while displaying in jquery: $(':checked').each(function()
I am C++ programmer and I need to make some changes to VB6 code.
I need to make use of some OWL ontologies in c#. Does anyone have
I need to write application in .NET which will be make some calculations on
I need to be able to be able to make some text on terminal
I need to make a WebView show some web pages where some parts are

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.