Jump to content

Starting XML Parser In A Method Possible?


3 posts in this topic

Recommended Posts

is it possible to call/start XML parsing inside a method?

 

 

 

- (MasterList *)processWebService:(NSString *)urlItemNumber
{
   status = [[NSMutableString alloc] init];
   mySingleItem = [[MasterList alloc] init];

   [status appendFormat:@"http://store.leestools.com:90/ios12/n_ios.asmx/"];
   [status appendFormat:@"getiteminfo?scode="];
   [status appendFormat:@"%@", urlItemNumber];

   NSString *urlAddress = status;
   NSURL *url = [NSURL URLWithString:urlAddress];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

   mySingleItem.masterDescript = @"Fake Descript";


   if (connection)
   {
    self.responseData = [NSMutableData data];

   }
   else
   {
    NSLog (@"The connection failed");
   }


   return mySingleItem;

}

 

 

is it possible to call XML parsing somewhere in this method?

 

 

I'm making a split view APP, and i created a separate .h and .m files to put parsing in there because, basically I'm using XML parsing in my "master View controller (table view)" and populates my tableview with products, and I'm creating this object to call XML parsing for all the detail data for "detail view controller"

 

 

but the XML parsing doesnt get called right away, in which makes all my strings nil

Link to comment
Share on other sites

Firstly and foremost, hopefully you're using ARC or your code is leaking like a sieve ;).

 

And to the main point. The NSURLConnection initializer initWithRequest:delegate: makes an asynchronous url connection which must use the delegate methods to get the data. So if you are intending to/must parse the downloaded data in that method, you will need to use the synchronous variant sendSynchronousRequest:returningResponse:error:.

 

IMO, I would avoid using the synchronous variant as it is less flexible, a pretty good explanation here: http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/ and also because it requires some threading to not block the main thread - something which you should try to avoid unless you have a very good plan.

 

So in short I would rethink that method and how you want to load the data. Hacky solutions now make for painful maintenance later ;).

Link to comment
Share on other sites

+1 with the ARC hope you had the box ticked when you created your project initially. (Xcode should warn you about non dereferenced ivars though)

 

For the topic on hand, iPoco basically gave you your options and I'm backing the asynchronous method simply because when it comes to downloading, under normal circumstances you don't want to sync it which would require threading and more of a chance that the parsed data might not come through in the event that another thread crashes your app. (Things happen unexpectedly better not open the possibility for it) I personally wouldn't go for a hacky solution to download information via the Internet because I always look at the data being crucial and the odds of the data getting lost along the way due to the non standard way of getting files. You're probably better off going down the aync method although it's a little more cumbersome than sync, it's rewards are undoubtedly better.

 

Only thing that really comes to mind as a hacky like method off the top of my dome is using a Singleton macro which I've done on occasions but like I said when it comes to downloading data I don't like to take risks on that.

 

Just my two cents though.

  • Like 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...