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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:31:28+00:00 2026-06-18T10:31:28+00:00

I am using following Code to get response from a string query. There are

  • 0

I am using following Code to get response from a string query. There are a lot of queries all around in my app and i want to copy and paste this code again and again

Is there any way I can just make an instance of it , pass the urlString and then return the response..

I have tried creating a function
+(NSString*) myFunc{} in an NSObject Class but it seems that GCD Doesn’t work except Main UI Threads. How can i fix this issue

__block__  NSString *response;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    //your server url and request. data comes back in this background thread
    response; = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];

    dispatch_async(dispatch_get_main_queue(), ^{
        //update main thread here.
        NSLog(@"%@",response); // NEED TO RETURN THIS

        if (err != nil)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Error"
                                                           message: @"An error has occurred."
                                                          delegate: self
                                                 cancelButtonTitle:@"Ok"
                                                 otherButtonTitles:nil];
            [alert show];
            [indicator stopAnimating];
        }
    });
});
  • 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-06-18T10:31:29+00:00Added an answer on June 18, 2026 at 10:31 am

    I would separate out the request processing from the error reporting, using a completion block to provide the feedback to the caller.

    First define the completion block semantics; we know we want the string response and an optional error descriptor:

    typedef void (^COMPLETION_BLOCK)(NSString *response, NSString *error);
    

    Second implement the method that will get the response in the background and then call the completion block in the main thread. This could be a class method in some global utility class if you wish:

    - (void)responseFromURL:(NSURL *)url
            completionBlock:(COMPLETION_BLOCK)completionBlock
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            NSError *error = nil;
            NSString *response = [NSString stringWithContentsOfURL:url
                                                          encoding:NSUTF8StringEncoding
                                                             error:&error];
    
            dispatch_async(dispatch_get_main_queue(), ^{
                completionBlock(response, error);
            }
        }
    }
    

    And finally call the method:

    [someClass responseFromURL:[NSURL URLWithString:queryString]
               completionBlock:^(NSString *response, NSError *error) {
    
        NSLog(@"response='%@'", response);
    
        if (error != nil)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error getting response"
                                                           message:[error localizedDescription]
                                                          delegate:self
                                                 cancelButtonTitle:@"Ok"
                                                 otherButtonTitles:nil];
            [alert show];
            [indicator stopAnimating];
        }
    }];
    

    (this code is untested and will therefore doubtless contain some bugs)

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

Sidebar

Related Questions

I'm using the following code to get response from a requested page using php
I'm using the following code to get an array with all sub directories from
I am using following code to get bitmap from url. This function is used
I'm using the following code to get a list of all installed apps on
I am using the following code to get an XML data from a website
I get following XML string as a response from BING (Note:COMPOSITE result), i tried
I am using following code to get data from the Server and if the
My service side code is implemented using Resteasy @GET @Path(/ad-details/{query}) @Produces(application/json) public String getAdDetails(@PathParam(query)
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I'm using the following code to execute the query. I want to display the

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.