如何在控制台中捕获NSXMLParser的错误?
我的项目正在使用正式的“LazyTableImages”。
问题是它不能一直工作,表没有任何价值。
这是我为获取一些调试信息所做的工作:
放置“cachePolicy:0和timeoutInterval:160.0”以避免网址超时:
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:TopPaidAppsFeed] cachePolicy:0 timeoutInterval:160.0]
然后添加“NSLog(@”data:%@“,string)”以检查我是否有xml当前:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.dataToParse];
NSString *string = [[NSString alloc] initWithData:self.dataToParse encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", string);
[parser setDelegate:self];
[parser parse];
控制台在启动时显示xml,其他任何东西都可以:
2014-02-24 11:54:30.566 MyXMlParserTest[1419:1403] data: <?xml version="1.0" encoding="UTF-8"?>
之后我把它放在parseError上以检查错误但没有发生任何事情:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSString * errorString = [NSString stringWithFormat:@"Unable to download data (Error code %i )",[parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
然后我把“NSLog(@”nodeCount)“放在connectionDidFinishLoading上:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.appListFeedConnection = nil; // release our connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// create the queue to run our ParseOperation
self.queue = [[NSOperationQueue alloc] init];
// create an ParseOperation (NSOperation subclass) to parse the RSS feed data
// so that the UI is not blocked
ParseOperation *parser = [[ParseOperation alloc] initWithData:self.appListData];
parser.errorHandler = ^(NSError *parseError) {
dispatch_async(dispatch_get_main_queue(), ^{
[self handleError:parseError];
});
};
// Referencing parser from within its completionBlock would create a retain
// cycle.
__weak ParseOperation *weakParser = parser;
parser.completionBlock = ^(void) {
if (weakParser.appRecordList) {
// The completion block may execute on any thread. Because operations
// involving the UI are about to be performed, make sure they execute
// on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
// The root rootViewController is the only child of the navigation
// controller, which is the window's rootViewController.
RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
rootViewController.entries = weakParser.appRecordList;
NSUInteger nodeCount = [rootViewController.entries count];
NSLog(@"nodeCount: %lu", (unsigned long)nodeCount);
// tell our table view to reload its data, now that parsing has completed
[rootViewController.tableView reloadData];
});
}
// we are finished with the queue and our ParseOperation
self.queue = nil;
};
[self.queue addOperation:parser]; // this will start the "ParseOperation"
// ownership of appListData has been transferred to the parse operation
// and should no longer be referenced in this thread
self.appListData = nil;
}
然后它返回:
014-02-24 11:54:30.582 MyXMlParserTest[1419:a0b] nodeCount: 0
从这一点开始,我不在哪里检查。
我在真实和模拟器设备上尝试了这一点并且发生了相同的结果。
十分之一的时间它工作和表填充。 9其他时间什么也没发生。
xml的字符串标记始终相同。只有图片网址标记正在发生变化。
答案 0 :(得分:0)
改变这一行后确定:
__weak ParseOperation *weakParser = parser;
以下任何一个:
ParseOperation *weakParser = parser;
或
__block ParseOperation *weakParser = parser;
问题消失了。