开发者

Parsing my flat-file on iPhone is a pain in the ***, please Help me out

开发者 https://www.devze.com 2023-01-14 02:22 出处:网络
I am programming an 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 an UITableView.

I am programming an 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 an 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 a 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:[[m开发者_StackOverflow中文版emberArray 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: -)


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];
    }
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消