Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-06-2011, 11:56 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 19
JJSaccolo is on a distinguished road
Default User Interface hangs when parsing

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
JJSaccolo is offline   Reply With Quote
Old 03-06-2011, 04:33 PM   #2 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

You need to look through the documentation for TBXML to see how to load from a URL asynchronously. The method you are using makes the UI thread wait until the XML is loaded from the remote server.
__________________
My development blog: http://jrinn.com
JasonR is offline   Reply With Quote
Old 04-13-2011, 02:49 PM   #3 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

I dident really look into all your code you posted but i think what you want to do is free the main thread of any type of parsing data so the user can respond to the program. all you have to do is look into grand central dispatch, this is how i did it with my program and now it moves flawlessly.

just follow these steps and this should work out for you
1)Create a queue to run your functions on
2)Call an asynchronous request of the q so it will run on its on thread, and your program will not have to wait for the funciton's to be done in order to continue, so your user can still interact with the program
3)update the UI with the main thread when you are done with parsing the data, call this line of code to update your new data to the UI. If you dont update the data on the main thread you have the possibility of having to wait a long time to see the results of your parsing

ex
Code:
-(void)parseData{
    1)
    dispatch_queue_t q = dispatch_queue_create("My Q",NULL);
    
   2)
   dispatch_async(q,^{
          [self parseMyData];

          3)
          dispatch_async(dispatch_get_main_queue(),^{
               [self updateUI];
          });
   )}: 
   dispatch_release(q);   
}
I hope this helps and heres the link to the GCD reference guide on apple

GCD Reference Guide

Last edited by Esko2300; 04-14-2011 at 06:55 PM.
Esko2300 is offline   Reply With Quote
Old 10-20-2011, 10:52 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 113
jonusx is on a distinguished road
Default

You also probable don't want to use NSUserDefaults for saving data like that. Write the array to the filesystem. NSUserDefaults isn't intended for stuff like that
__________________
What do you have to share? Share and view pics Oneshare

Play Magic: The Gathering? Try The Sylvan Archives!

Warhammer 40k your thing? W40k Manager is your thing.

Like karaoke? Try iSing Karaoke Locator
jonusx is offline   Reply With Quote
Reply

Bookmarks

Tags
parse xml, tableview, user interface, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
18 members and 384 guests
Brandt, coolman, Domele, Droverson, Duncan C, fredidf, Free App Monster, givensur, iAppDeveloper, locombiano89, Mah6447, Meoz, simplymuzik3, SLIC, stanny, stevenkik, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,864
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0