Question

My app parses an online XML file. I am trying to add a feature that will scan the LINK from each item in the xml for certain keywords, and return them to an NSString. I set it up to do this when it parses using:

 NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:articleUrl] encoding:NSUTF8StringEncoding error:nil];
            NSMutableArray *substrings = [NSMutableArray new];
            NSScanner *scanner = [NSScanner scannerWithString:string];
            [scanner scanUpToString:@"Thought:" intoString:nil]; 
            while(![scanner isAtEnd]) {
                NSString *substring = nil;
                [scanner scanString:@"Thought:" intoString:nil]; 
                if([scanner scanUpToString:@"</body>" intoString:&substring]) {

                    [substrings addObject:substring];
                }
                [scanner scanUpToString:@"Thought:" intoString:nil]; 
            }
            [substrings release];

The issue is that the XML has many items, this takes time, and the TableView cells don't appear until after this task is completed. Is there a quicker way that could work?

Was it helpful?

Solution

Quicker will be very relative. (file size, number of things to search for, algorithm used, etc...) You might try a few other approaches. NSXMLParser is another one. NSRegularExpression is another. It might be more efficient to first capture all the links then parse them. But again, that's a very general statement.

To keep your UI responsive while parsing, create a subclass of NSOperation and use that operation in the background. Have the NSOperation subclass send a Notification on the main thread when complete (or for each item added to your data structure). Make your app delegate (or whatever controller is appropriate) an observer for the Notification. You can have a separate Notification for completion and leverage that to provide a progress indicator stop.

This gives you a responsive UI, callbacks and hooks to update with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top