垂悬在背景rss解析的UI

时间:2013-10-16 12:22:57

标签: ios nsurlconnection nsurlrequest nsoperationqueue

我正在尝试创建一个简单的rss阅读器。代码工作正常,除了在更新订阅源时UI挂起。我认为我拼凑了代码来获取feed并在更新mainQueue上的UI时将其解析为后台队列,但是表格挂起非常糟糕。代码如下:

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

调用refreshFeed2的代码:

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

任何帮助?

谢谢!

1 个答案:

答案 0 :(得分:1)

你能试试吗?取代

[self refreshInvoked:self forState:UIControlStateNormal];

通过

[self performSelectorOnBackground:@selector(refreshFeed2) withObject:nil];

并替换

而不是

 -(void) refreshInvoked:(id)sender forState:(UIControlState)state {
     [self performSelectorOnBackground:@selector(refreshFeed2) withObject:nil ];
    }