views:

76

answers:

3

Hey guys,

stack overflow has already helped me a lot during my programming work so at first: Thanks a lot for all the asked questions and the excellent answers.

Nevertheless I have a problem right now, for which I just couldn't find the right answer here.

I am programming a Iphone App which is supposed to parse a flat-file from the web, create managed objects from the flat-file and later on should display them in a UITableView. There are no problems with the saving and the displaying, but I just can't get the hang of a good Parser.

Thats the file I want to parse: Flat-file

AS far as I know, I can't use the NSXMLParser for this task (because obviously there are no tags).

So i at first tried to programm an NSScanner which should get me the interesting properties --> didn't work out

now I am using this method:

- (void) parseMemberDataWithURL: (NSString *)urlString
{
    self.memberTempCounter = 1;

    //Get data from web
    self.downloadedText = [NSString stringWithContentsOfURL: [NSURL URLWithString:    urlString] encoding:NSUTF8StringEncoding error:nil ];

    memberArray = [downloadedText componentsSeparatedByString:@";"];

    while (self.memberTempCounter<[memberArray count])
    {
        [[ExhibitorController sharedController] createExhibitorWithName:[memberArray objectAtIndex:self.memberTempCounter]
                                                                 street:[memberArray objectAtIndex:self.memberTempCounter+2]
                                                                    zip:[memberArray objectAtIndex:self.memberTempCounter+3]
                                                                   city:[memberArray objectAtIndex:self.memberTempCounter+4]
                                                                  email:[memberArray objectAtIndex:self.memberTempCounter+7]
                                                                  phone:[memberArray objectAtIndex:self.memberTempCounter+5]
                                                                website:[memberArray objectAtIndex:self.memberTempCounter+8]
                                                        produktbereiche:[[memberArray objectAtIndex:self.memberTempCounter+9] componentsSeparatedByString:@","]];
        self.memberTempCounter= self.memberTempCounter+13;
    } 
}

I am using the memberTempCounter to identify the property.

The problems are:

  • This only works out in like 3 of 4 times. 1 of 4 times the App crashes and I have no Idea why....
  • The method has a performance like a 1962 VW Beetle. Parsing the whole chunk of data takes up to 3 Minutes on my Iphone 3G

Any Ideas or a simpler way to do this?

I would be really gratefull. Thanks in advance :-)

A: 

Recursive-descent (LL1) parsers are pretty simple, light on memory, and for speed they go almost as fast as you can run a pointer through characters. Building your data structure would probably be the dominant time-taker.

Mike Dunlavey
+1  A: 

You might as well do all the parsing in the background, and then display as the information gets parsed.

As for memory issues, try doing temporary autorelease pools and release every 50 or so iterations through the loop.

int count = 0;
NSAutoreleasePool * loopPool = [[NSAutoreleasePool alloc] init];
while(someInsanelyLargeCondition){

    // Do your stuff here
    // .............

    count++;
    if (count > 50) {
        count = 0;
        [loopPool release];
        loopPool = [[NSAutoreleasePool alloc] init];
    }
}
David Liu
Thx. I don't get the Idea of an Autoreleasepool. I don't release anything during my method but write the data directly to the data-base. Lately I was thinking about Parsing the files, store them in a temporally NSArray to display them faster and handle the Saving via Background Thread. Any problems coming to your mind using this method? Or what about creating 2 additional Threads for saving the parsed data into the database ?
Amandir
Although you don't autorelease, anything within any methods that you call could be using autorelease.
David Liu
A: 

Hey Guys,

I was finally able to fix my performance problem.

I have a method in another class, which ads Tags for the different Exhibitors. Therefore it first checks if the Tag already is stored in the database or else creates it. With an growing Set of Tags in my database the search-process took longer and longer and this led to the long parsing time.

Anyone else having this problem: Take a look at the Performance Core Data Programming guide of apple in the "Implementing Find-or-Create Efficiently"-section:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html

Amandir