Hi everyone!
i'm sorry for my bad english, i'm not sure to use the correct verbs and pronouns!
So: my app has a UITableView and a button "reload".
Touching the button "reload" it starts to parse an xml file, that contains the data that i need to put in the tableview.
the xml is similar like this:
Code:
<abcs>
<abc id="001" label="hello" />
<abc id="002" label="ciao" />
...
</abcs>
i'm using TBXML and this is how it works: i created two NSMutableArray (id and label), and i add the data into the arrays.
So, at the end of the parse, i have this arrays:
id[0] = "001"
id[1] = "002"
...
label[0] = "hello"
label[1] = "ciao"
This is useful because when the user touch a line of the UITableView, i know what data read! If the user touch the line 0, i need to read id[0] and label[0].
After the parse i call the method "reloadData" for the tableview.
What's the problem?
That during the parse, the User Interface is blocked!
For example i've insert this
Code:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
and, at the end, this
Code:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
it appears only for a moment (at the end).
Why? what i'm doing wrong?
NB: i'm using the "NSUserDefaults" for save my arrays, so the user can read the data offline!
this is my code:
Code:
-(void)viewDidLoad{
NSString *save = [[NSUserDefaults standardUserDefaults] objectForKey:@"save"];
if (!save){
id = [[NSMutableArray alloc] init];
label = [[NSMutableArray alloc] init];
}
else{
id = [[NSUserDefaults standardUserDefaults] objectForKey:@"save_id"];
label = [[NSUserDefaults standardUserDefaults] objectForKey:@"save_label"];
}
}
//here the methods to load the tableview
-(IBAction)reload{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"URL_OF_THE_XML_FILE"];
TBXML *tbxml_parse = [[TBXML alloc] initWithURL:url];
if (tbxml_parse.rootXMLElement) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"SI" forKey:@"save"];
[self traverseElement:tbxml_parse.rootXMLElement];
//i save the arrays
[defaults setObject:id forKey:@"save_id"];
[defaults setObject:label forKey:@"save_label"];
[mytableview reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[tbxml_parse release];
}
else //UIAlertView "error, no connection"!
}
- (void) traverseElement:(TBXMLElement *)element {
int sent_id = 0;
int sent_label = 0;
array_id = [[NSMutableArray alloc]init];
array_label = [[NSMutableArray alloc]init];
do {
TBXMLAttribute * attribute = element->firstAttribute;
// if attribute is valid
while (attribute) {
NSString *xml_id = [NSString stringWithFormat:@"%@", [TBXML attributeName:attribute]];
if ([xml_id isEqualToString:@"id"]){
[array_id insertObject:[NSString stringWithFormat:@"%@", [TBXML attributeValue:attribute]] atIndex:sent_id];
sent_id++;
}
NSString *xml_tipo = [NSString stringWithFormat:@"%@", [TBXML attributeName:attribute]];
if ([xml_tipo isEqualToString:@"label"]){
[array_tipo insertObject:[NSString stringWithFormat:@"%@", [TBXML attributeValue:attribute]] atIndex:sent_label];
sent_label++;
}
attribute = attribute->next;
}
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
}
thank you