0

我正在尝试创建一个简单的 rss 阅读器。代码可以正常工作,只是在更新提要时 UI 挂起。我以为我拼凑了代码以获取提要并在后台队列上解析它,同时更新 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];
}];
}

有什么帮助吗?

谢谢!

4

1 回答 1

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 ];
    }
于 2013-10-16T14:20:14.597 回答