Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashwinr87

macrumors member
Original poster
Mar 9, 2011
81
0
In my iPAD application, I call a webservice to get a XML response, then I parse it and store it into my SQLITE database.

The parsing and saving are happening properly, but the problem I am having is that it is taking a very long time for it to perform the operation.

Using the mac, I saw the number of records being saved to the database. It was 395 rows where each row has 8 columns in it i.e. around 3100 records are being stored to my database.(my entity has 8 attributes in it). On the iPAD, it takes about 25 seconds to do the whole operation, which I was told is too long. I am unable to figure out why it is taking so long and where I am going wrong.

This is the code which I use for parsing the XML and storing -

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"return"])
    {
        // Blank lab panel object
        objLabPanel = [NSEntityDescription insertNewObjectForEntityForName:@"LabPanels" inManagedObjectContext:managedObjectContext];
        mainElement = elementName;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
    elementValue = [[NSMutableString alloc] init];
    [elementValue appendString:string];

    // Handle html codes
    elementValue = [CommonHelper encodeHTMLCharactorsForDataBaseStorage:elementValue];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"return"])
    {
        [objPatient addLabPanelsObject:objLabPanel];

        // Save
        NSError *error = nil;
        BOOL saveObj = FALSE;
        saveObj = [managedObjectContext save:&error];

        if (saveObj == FALSE)
        {
            NSLog (@"Error: %@", error);
        }
    }
    else if ([elementName isEqualToString:@"batteryID"] && [mainElement isEqualToString:@"return"])
    {
        objLabPanel.labPanelBatteryId = elementValue;
    }
    else if ([elementName isEqualToString:@"batteryVersionNum"] && [mainElement isEqualToString:@"return"])
    {
        objLabPanel.labBatteryVersionId = elementValue;
    }
    else if ([elementName isEqualToString:@"conceptCode"] && [mainElement isEqualToString:@"return"])
    {
        objLabPanel.labPanelCode = elementValue;
    }
    else if ([elementName isEqualToString:@"conceptDescription"] && [mainElement isEqualToString:@"return"])
    {
        objLabPanel.labPanelDesc = elementValue;
    }
    else if ([elementName isEqualToString:@"effectiveEndTime"] && [mainElement isEqualToString:@"return"])
    {
        endDate = [CommonHelper getDateFromXMLString:[NSString stringWithString:elementValue] :@"yyyy-MM-dd'T'HHmmssZ"];
        objLabPanel.labPanelEndDate  = endDate;
    }
    else if ([elementName isEqualToString:@"effectiveStartTime"] && [mainElement isEqualToString:@"return"])
    {
        startDate = [CommonHelper getDateFromXMLString:[NSString stringWithString:elementValue] :@"yyyy-MM-dd'T'HHmmssZ"];
        objLabPanel.labPanelStartDate = startDate;
    }
    else if ([elementName isEqualToString:@"body"])
    {
        // Release all variables at the end of xml parsing
        [self releaseVariables];
    }

    elementValue = nil;

It would be great if someone could help me out with this and tell me if something is wrong with the way I am parsing and saving.
 
Did ou tried Instruments to get a profiling on your code on the device. That would give you ood hint where to search for performance issues.

I have used SQLite on iPad but not with high workload. I'm quite happy and use the FM class as wrapper.

Something you could start with an easy optimization would be to make the comparison for mainElement only once and store the result in a BOOL or nest the comparison with an additional if().
But avoid repeated string comparison; those in XML can be expensive as often will fail until the code found a match.

You could also make hash codes on elementName and mainElement and use those for comparison; much faster when lots of data rows are expected.

But again: run Instrument will give you very good hints. You can also share some infos here if you want.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.