Returning data from a data capture class from the Internet?

I am trying to create a class that will allow me to retrieve the requested data from a web service. I went in cycles on how to return 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, he should get sports news. The problem is that the DataGrabber receives data asynchronously and finishes jumping from several methods of the NSURLConnection delegate. How to find out in FooClass when the data was received?

+1
source share
3 answers

, , (, DataGrabber , NSURLConnection , ?). -API, XML JSON .

// 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]];
}

DataGrabber.h:

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

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];
}

, Foo DataGrabberDelegate .

, DataGrabber delegate (, , , ):

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

NSURLConnection DataGrabber, UIViewController , . , DataGrabber , " " - API View Controller . , " " ( , JSON XML-).

- , - , UIActivityIndicator .., , , .

, : -API iPhone

+3

DataGrabber, , ( , ), (. ) , , , .

: , FooClass DataGrabber

+1

. , .

0

Source: https://habr.com/ru/post/1769542/


All Articles