gpt4 book ai didi

ios - UI卡在后台rss解析

转载 作者:行者123 更新时间:2023-11-29 03:34:07 33 4
gpt4 key购买 nike

我正在尝试创建一个简单的 rss 阅读器。代码工作正常,只是更新 feed 时 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];
}];
}

有什么帮助吗?

谢谢!

最佳答案

你能试试这个吗?替换

[self refreshInvoked:self forState:UIControlStateNormal];

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

并替换为相同的而不是

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

关于ios - UI卡在后台rss解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403294/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com