Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-31-2011, 08:43 AM   #1 (permalink)
learner2011
 
Join Date: Mar 2011
Posts: 115
ashwinr87 is on a distinguished road
Default Bulk storing in SQLite while parsing in iOS

In my iOS application, I am parsing an XML from a SOAP based webservice and I am storing it into the SQLITE database. The problem that I am facing it is that, it is very slow. It takes about 18 seconds for about 310 rows of data.

Here is what is a sample of my XML -

Code:
<return>
<batteryID>1234</batteryID>
<batteryVersionNum>1</batteryVersionNum>
<conceptCode>abc</conceptCode>
<conceptDescription>abc</conceptDescription>
<effectiveEndTime>2010-11-23</effectiveEndTime>
<effectiveStartTime>2010-11-23</effectiveStartTime>
</return>
<return>
<batteryID>2345</batteryID>
<batteryVersionNum>1</batteryVersionNum>
<conceptCode>bac</conceptCode>
<conceptDescription>bac</conceptDescription>
<effectiveEndTime>2010-11-23</effectiveEndTime>
<effectiveStartTime>2010-11-23</effectiveStartTime>
</return>
I use NSXML parser for parsing the XML. For every return tag() I encounter, I create a new instance of my entity -

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;
    }
}
Once I encounter a return end element (), I save the object to the database -
Code:
- (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;
    }
    // getting other values from the xml
    . 
    .
    .
    }
Hence for every return tag I encounter, I do a save to the database and so for my 310 or so records, I would be saving it 310 times. So by doing a [managedObjectContext save:&error]; , I guess that it opens the database, saves the data and closes the database and hence it takes a lot of time. Am I right in thinking this way?

Is there a way I can save all the objLabPanel's to an array and then do a bulk insert to the database i.e. bulk insert the entire array of objLabPanel's into the SQLITE database at once?

It would be great if someone could help me out with this.
ashwinr87 is offline   Reply With Quote
Old 10-31-2011, 09:20 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

You should save the managedObjectContext only once , after all your inserts are done. That will speed up the process a lot. If you expect the number of entries to grow in the future, you can save your context every 500 records added or so, to be on the safe side.
nobre84 is offline   Reply With Quote
Old 10-31-2011, 09:35 AM   #3 (permalink)
learner2011
 
Join Date: Mar 2011
Posts: 115
ashwinr87 is on a distinguished road
Default

Thank you very much.. I just tried that and found out it is working..
one question.. how would I save it every 500 records or so? I would need to keep a kind of counter or something right?

Quote:
Originally Posted by nobre84 View Post
You should save the managedObjectContext only once , after all your inserts are done. That will speed up the process a lot. If you expect the number of entries to grow in the future, you can save your context every 500 records added or so, to be on the safe side.
ashwinr87 is offline   Reply With Quote
Old 10-31-2011, 01:36 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

If the data itself doesn't have any numbering info you can use, you can declare a static int inside the function to count it... Every time you reach 500 , you save the context and set it to zero. Be sure to still save the context at the end of the parsing, because you could be in the middle of a 500-batch and still didn't save before finishing
nobre84 is offline   Reply With Quote
Old 10-31-2011, 02:03 PM   #5 (permalink)
learner2011
 
Join Date: Mar 2011
Posts: 115
ashwinr87 is on a distinguished road
Default

yeah.. that was what I was thinking about doing too..
thank you for the reply...

Quote:
Originally Posted by nobre84 View Post
If the data itself doesn't have any numbering info you can use, you can declare a static int inside the function to count it... Every time you reach 500 , you save the context and set it to zero. Be sure to still save the context at the end of the parsing, because you could be in the middle of a 500-batch and still didn't save before finishing
ashwinr87 is offline   Reply With Quote
Reply

Bookmarks

Tags
coredata, ios, managedobjectcontext, sqlite

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
13 members and 390 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:31 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0