views:

604

answers:

3

Hello, i'm building an app that will read rss feeds and will present them in UITableViews. I searched on google and here and ai decided to use LibXML.

When my app starts it checkes if it has a valid internet connection, and grabs an rss feed from which i extract some data and i make an uiTabBar with tableViews. after parsing the first rss feed and building the UITabBar, for each button in my uiTabBar i have a different rss feed(category). When i have a good internet connection (wifi) and even 3G my app works really good but when the internet connection suffers my app does too.

Because i parse all the feeds one after another and in the main thread the GUI just stops responding and it is a problem.

I use LibXML for parsing my feeds, i do it like this:

CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];

NSArray *resultDate = NULL;
resultDate = [rssParser nodesForXPath:@"//channel" error:nil];

for(CXMLElement *date in resultDate)
{
 NSString *data = (NSString *)[[date childAtIndex:12] stringValue];
 [self setLastBuildDate:[[self parseDate:data] copy]];
 break;
}

NSArray *resultNodes = NULL;
resultNodes = [rssParser nodesForXPath:@"//item" error:nil];

for(CXMLElement *resultElement in resultNodes)
{
 NSMutableDictionary *feed = [[NSMutableDictionary alloc] init];
 int counter;

 for(counter = 0; counter < [resultElement childCount]; counter++)
 {
  [feed setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
 }
 [links addObject:[feed objectForKey:@"link"]];
 [feedsList addObject:[feed copy]]; 
 [feed release];
}
[newsTable reloadData];

I tried to parse only the feed for the first button and the other ones when i push the button on the uitabbar but i can't call the functions that grab and parse the feed on -didselectitem event.

Now i was thinking of parsing them in another thread not in the main one.. can someone give me some pointers?

thank you in advance

+1  A: 

Look into the NSOperation and NSOperationQueue. They are very useful for multithreading.

Basically subclass NSOperation and write your code into the main method. Then add your operation to an operation queue, and each operation will be run on another thread.

You can set up callbacks when your operations complete, so you can feed the parsed data back to your views.

Jasarien
it works, thank you.
SorinA.
+4  A: 

The basic issue is that initWithContentsOfURL: blocks until it gets the data from the web, which can take seconds. If you call it on the main thread of your app, it basically locks up the entire app for the duration of the network traffic.

Another option would be to use NSURLConnection to pull down the data. It's not as convenient as the single method call of initWithContentsOfURL:, but is inherently asynchronous. So, rather than having to implement the threading and callbacks on your own, you just implement a couple of delegate methods, and then create instances of NSURLConnection.

See the official documentation for the URL loading system for more information.

Sixten Otto
A: 

I've released an open source RSS/Atom Parser for iPhone and it makes reading and parsing web feeds extremely easy.

You can set it to download the data asynchronously, or you could run it in a background thread synchronously to collect the feed data. That way while the data is being parsed you UI will be free.

Michael Waterfall