Kjeld
Kjeld

Reputation: 83

XML Parser Xcode really slow

I'm developing an iPhone application and have some trouble with my xml parser. I have to check multiple values from multiple XML files, but when the XML parser is active I can't do anything else. This is not how I want it, because checking the xml must be done in the background, without being noticed. Here is some of my code, hope it's enough!

appDelegate.datavalues = [[NSMutableArray alloc] init];
for(int i = 0; i < [headarray count]; i++){
    NSMutableArray *infoarray = [[NSMutableArray alloc]initWithArray:[headarray objectAtIndex:i]];
    NSString *IP = [infoarray objectAtIndex:1];
    NSString *Unique = [infoarray objectAtIndex:2];
    NSString *Port = [infoarray objectAtIndex:3];
    NSString *relay = (NSString *)[infoarray objectAtIndex:4];
    NSString *input = (NSString *)[infoarray objectAtIndex:5];
    NSLog(@"relay%@",relay);
    NSString *urlAddress = [NSString stringWithFormat:@"http://%@:%@/state.xml",IP,Port];
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSString *authHeader = [NSString stringWithFormat:@"Basic %@",Unique];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval: 3];
    [request setValue:authHeader forHTTPHeaderField:@"Authorization"];
    //NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSURLResponse *myURLResponse;
    NSError *myError;

    NSData* myDataResult = [NSURLConnection sendSynchronousRequest: request returningResponse:&myURLResponse error:&myError];


    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myDataResult];
    XMLParser *parser = [[XMLParser alloc] initXMLParser];
    //parser.relay = [infoarray objectAtIndex:4];
    //Set delegate
    [xmlParser setDelegate:parser];
    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

after this I check some values so I don't think that's necessary to show!

Upvotes: 1

Views: 787

Answers (2)

zoul
zoul

Reputation: 104065

You could refactor the XML-related code into a separate method and then you can use Grand Central Dispatch to run the method in background:

- (void) startOperation
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_NORMAL, 0), ^{
        [self runSomeXMLChecks];
        dispatch_sync(dispatch_get_main_queue(), ^{
            // This is dispatched on the main queue so that
            // you can update the UI. The NSLog is just an example.
            NSLog(@"XML check done!");
        });
    });
}

Upvotes: 1

picciano
picciano

Reputation: 22701

Have a look at the NSOperation and NSOperationQueue APIs and/or the Concurrency Programming Guide. (Both are in the Xcode library).

From the docs:

The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

Upvotes: 0

Related Questions