I have created the following method in cocoa:
-(NSArray *)latestData
{
NSURL *requestingURL = [NSURL URLWithString:@"someRestfulURL"];
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:requestingURL];
[theRequest setHTTPMethod:@"GET"];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data]retain];
}
else
{
NSLog(@"the connection is NULL");
}
return someArray;
}
The RESTful webservice I am calling returns XML that I parse using NSXMLParser.
How can I return an array when calling latestData if I have to wait for the delegate methods of NSURLConnection and NSXMLParser to finish before I can fill the array with the appropriate data?
Few things you can do
1. Make the request synchronous, this way you will have the array at the time of return.
2. If you want to make it asynchronous, instead of returning the array, have the array as a class member and fill it in the finishedRequest delegate method.