我正在从我的网络服务器解析两批数据,我遇到了一些问题,但我认为我已将其锁定在我在parserDidEndDocument方法中找到的一个问题。
一旦我得到 if(dataSetToParse == @“vent”)我就陷入了这个循环,如果我记录结果它只是不断地保持打印相同的结果集记录一遍又一遍。
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
if (dataSetToParse == @"shaft"){
//Filter results shaft (isShaft = T)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"isShaft",@"T"];
NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
//Load filteredArray into shaftFilterArray, which can be used outside of this method and passed to other views
shaftFilterArray = filteredArray;
[self.tableView reloadData]; //Reloads the tabel delegate methods
}
if (dataSetToParse == @"vent"){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ventID",isShaftString]; //isShaftString is being set from the sub view when the user makes a isShaft selection
NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
if ([filteredArray count] == 0){ //<<---- getting stuck in this if statment
self.removeActivityIndicator = NO; // sets indicator to be turned off once tableview has been reloaded, (tableView:cellForRowAtIndexPath:) and there are no values in the array i.e.(manufacture has no modles)
}else if([filteredArray count] !=0){
ventFilterArray = filteredArray; // ventFilterArray is passed to subview to be displayed
}
[self setRequestString:@"drain.xml"]; //Sets up the rest for submodel data that needs to be sent to subview with vent data
[self.tableView reloadData]; //Reloads the tabel delegate methods
}
}
## UPDATE
我发现为什么我的解析器似乎陷入了无限循环,因为它是从我的连接委托无限地调用的。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
if (dataSetToParse == @"shaft"){
NSData *responseData = [shaftCachedResponse data];
[self startTheParsingProcess:responseData];
}
else if (dataSetToParse == @"vent"){
NSData *responseData = [ventCachedResponse data]; //<<--- this is where things are getting stuck! with no idea why.
[self startTheParsingProcess:responseData];
}
}
不确定为什么会这样。
##更新2
所以我跟踪了连接委托,看看他们被调用的顺序以及被调用的次数。这就是发生的事情
对于第一个dataSetToParse - @“shaft”它就像这样工作
完成 - 关闭并解析数据。
对于第二个dataSetToParse - @“vent”这就是发生的事情。
解析器委托被调用,因为它在第一种情况下发生,但随后连接委托再次开始但略有不同,因为现在缓存已设置。
依此类推......不断重复...... 有一个问题,我可以像我一样使用这些连接代理,设置两个不同的缓存吗?