Note that there are some explanatory texts on larger screens.

plurals
  1. POUI hanging on background rss parsing
    primarykey
    data
    text
    <p>I'm trying to create a simple rss reader. The code works okay, except the UI hangs when the feeds are being updated. I thought I cobbled together the code to get the feed and parse it on a background queue while updating the UI on the mainQueue, but the table hangs pretty badly. Code below:</p> <pre><code> -(void)refreshFeed2 { NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (NSString *feed in _feeds) { // iterate over all feeds NSLog(@"feed=%@", feed); NSURL *url = [NSURL URLWithString:feed]; // Create url connection and fire request NSURLConnection *conn = [[NSURLConnection alloc] init]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; (void)[conn initWithRequest:request delegate:self]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if ([data length] == 0 &amp;&amp; error == nil) { // handle empty response } else if (error != nil) { // handle error NSLog(@"Error %@", [error localizedDescription]); } else if ([httpResponse statusCode] == 200) { // data present and no errors [queue addOperationWithBlock:^{ // parse feed on queue RXMLElement *rss = [RXMLElement elementFromXMLData:data]; RXMLElement *rssChild = [rss child:@"channel"]; RXMLElement* title = [rssChild child:@"title"]; NSArray* items = [[rss child:@"channel"] children:@"item"]; NSMutableArray* result=[NSMutableArray array]; for (RXMLElement *e in items) { // iterate over the articles RSSArticle* article = [[RSSArticle alloc] init]; article.sourceTitle = [title text]; article.articleTitle = [[e child:@"title"] text]; article.articleDescription = [[e child:@"description"] text]; article.articleUrl = [NSURL URLWithString: [[e child:@"link"] text]]; NSString *articleDateString = [[e child:@"pubDate"] text]; article.articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822]; if (article.articleUrl != NULL) { [result addObject:article]; } } [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // update table on mainQueue for (RSSArticle *article in result) { // iterate over articles int insertIdx = [_allEntries indexForInsertingObject:article sortedUsingBlock:^(id a, id b) { RSSArticle *entry1 = (RSSArticle *) a; RSSArticle *entry2 = (RSSArticle *) b; return [entry1.articleDate compare:entry2.articleDate]; }]; [_allEntries insertObject:article atIndex:insertIdx]; [self.LeftTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; } }]; }]; } }]; // Stop refresh control [refreshControl endRefreshing]; } } </code></pre> <p>Code that calls refreshFeed2:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; self.allEntries = [NSMutableArray array]; self.feeds = [NSArray arrayWithObjects: @"http://feeds.washingtonpost.com/rss/politics", @"http://rss.cnn.com/rss/cnn_allpolitics.rss", @"http://www.npr.org/rss/rss.php?id=1012", @"http://www.slatedigital.com/support/feeds/rss_kb.php?s=fd5aa35e773dc3177b85a2126583f002", nil]; } //add refresh control to the table view refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged]; NSString* fetchMessage = [NSString stringWithFormat:@"Fetching Articles"]; refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:fetchMessage attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:11.0]}]; [self.LeftTableView addSubview: refreshControl]; [self refreshInvoked:self forState:UIControlStateNormal]; } -(void) refreshInvoked:(id)sender forState:(UIControlState)state { NSOperationQueue *refreshQueue = [[NSOperationQueue alloc] init]; [refreshQueue addOperationWithBlock:^{ [self refreshFeed2]; }]; } </code></pre> <p>Any help?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload