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

The Archive Base Latest Questions

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

I’m trying to create a class which will let me get requested data from

  • 0

I’m trying to create a class which will let me get requested data from a web service. I’m stuck on how to return the values.

// FooClass.m
// DataGrabber is the class which is supposed to get values
dataGrabber = [[DataGrabber alloc] init];
xmlString = [dataGrabber getData:[NSDictionary dictionaryWithObjectsAndKeys:@"news", @"instruction", @"sport", @"section", nil]];

In this example, it’s supposed to get the sports news. The problem is that the DataGrabber gets the data asynchronously and ends up hopping from several NSURLConnection delegate methods. How do know in FooClass when data has been received?

  • 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-17T17:37:14+00:00Added an answer on May 17, 2026 at 5:37 pm

    The delegate pattern used with a strict protocol is very useful for this (that’s how DataGrabber would find out when NSURLConnection is done, right?). I have written a number of Web APIs that consume XML and JSON information this way.

    // In my view controller
    - (void) viewDidLoad
    {
      [super viewDidLoad];
      DataGrabber *dataGrabber = [[DataGrabber alloc] init];
      dataGrabber.delegate = self;
      [dataGrabber getData:[NSDictionary dictionaryWithObjectsAndKeys:@"news", @"instruction", @"sport", @"section", nil]];
    }
    

    Then in your DataGrabber.h file:

    @protocol DataGrabberDelegate
    @required
    - (void) dataGrabberFinished:(DataGrabber*)dataGrabber;
    - (void) dataGrabber:(DataGrabber*)dataGrabber failedWithError:(NSError*)error;
    @end
    

    And in DataGrabber.m:

    - (void) getData:(NSDictionary*)dict
    {
      // ... Some code to process "dict" here and create an NSURLRequest ...
      NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
    }
    
    - (void) connectionDidFinishLoading:(NSURLConnection*)connection
    {
      // ... Do any processing with the returned data ...
    
      // Tell our view controller we are done
      [self.delegate dataGrabberFinished:self];
    }
    

    Then make sure that Foo is implements the DataGrabberDelegate protocol methods to handle each case.

    Finally, your DataGrabber has a delegate property (make sure you use assign, not retain to avoid retain cycles):

    @property (nonatomic, assign) id<DataGrabberDelegate> delegate;
    

    And when the NSURLConnection asynchronous loads are finished inside of DataGrabber, they call back to your UIViewController in the protocol laid out above so that you can update the UI. If it’s ONE request, you could theoretically get rid of DataGrabber and put it inside your view controller, but I like to “separate my concerns” – API and View Controller stay separate. It generates an extra layer, but it keeps “text processing code” out of the view controllers (specifically for JSON and XML parsing code).

    I’ve done this many times with success – one other key is that it’s good to provide the user with some feedback that a page is loading – turn on the activity indicator in the status bar, show them a UIActivityIndicator, etc., and then when your delegate callback comes back with either success or failure, you get rid of it.

    Finally, I’ve written a more detailed blog post about this: Consuming Web APIs on the iPhone

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker

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.