Hi guys, imagine an xml file with 5000 sets of data. While parsing , i NSLog an element and it returned me 5000 sets of data, but i have a loop that is inserting the data into the sqlite3 database. It seems to stopped inserting around after 400+. I looked around and found out that it gobbles up memory?in the leaks instrument, the responsible library was libsqlite3.dylib and the Responsible frame was sqlite3MemMalloc so how do i solve it now? below is my code. The method is in NSXMLParser didEndElement method.
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
MedicalBedAppDelegate *appDelegate = (MedicalBedAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([elementName isEqualToString:@"Status"]) {
//setup some globals
databaseName = @"MedicalBedDatabase.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
//Method Stackoverflow
sqlite3 *database;
NSInteger i;
NSString *p = [[[NSString alloc]init] autorelease];
NSString *p2 = [[[NSString alloc]init] autorelease];
NSString *str = [NSString stringWithFormat:@"INSERT INTO UsageData(Date,Time,NoOfMovementRegistered,BreathingRate,TimeSinceLastMovement,Status,bID) VALUES ('%@','%@','%@','%@','%@','%@','%@')",currentDate,currentTime,currentNoMove,currentNoBreathe,currentTimeSinceLastMovement,currentStatus,currentBedNo];
const char *sqlStmt = [str UTF8String];
sqlite3_stmt *cmp_sqlStmt;
NSString *str1 = [NSString stringWithFormat:@"INSERT INTO XMLEntryID(EntryID,bID) VALUES ('%@','%@')",currentEntryID ,p];
const char *sqlStmt1 = [str1 UTF8String];
sqlite3_stmt *cmp_sqlStmt1;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
for (i = 0; i<([appDelegate.beds count]) ; i++) {
Bed *aBedInstance = (Bed *)[appDelegate.beds objectAtIndex:i];
EntryID *aEntryIDInstance = (EntryID *)[appDelegate.entryids objectAtIndex:i];
p = aBedInstance.bedno;
p2 = aEntryIDInstance.bedID;
if ([p intValue] == [currentBedNo intValue]) {
if ([aEntryIDInstance.bedID intValue] == [currentBedNo intValue])
{
if ([aEntryIDInstance.entryID intValue] > [currentEntryID intValue] && [aEntryIDInstance.bedID intValue] == [currentBedNo intValue]) {
//NSLog(@"xmlEntryID is lower then dBCount");
}
else if ([aEntryIDInstance.entryID intValue] == [currentEntryID intValue] && [aEntryIDInstance.bedID intValue] == [currentBedNo intValue])
{
//This if else if statement is needed because if the dbCount == currentEntryID , it would still consider it
// to be dbCount < currentEntryID
//NSLog(@" IT IS EQUAL ");
}
else if ([aEntryIDInstance.entryID intValue] < [currentEntryID intValue] && [aEntryIDInstance.bedID intValue] == [currentBedNo intValue] ) {
////NSLog(@"dBCount at selectionScreen = %d",[[appDelegate.dBCount objectAtIndex:0] intValue]);
//NSLog(@"currentEntryID at selectionScreen = %d",[currentEntryID intValue]);
//NSLog(@"xmlEntryID is higher then dBCount");
if(sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL) == SQLITE_OK)
{
int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt, NULL);
((returnValue ==SQLITE_OK)? NSLog(@"INSERT into USAGEDATA SUCCESS") : NSLog(@"INSERT into USAGEDATA Fail"));
sqlite3_step(cmp_sqlStmt);
}
sqlite3_finalize(cmp_sqlStmt);
sqlite3_close(database);
if(sqlite3_prepare_v2(database, sqlStmt1, -1, &cmp_sqlStmt1, NULL) == SQLITE_OK)
{
int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt1, NULL);
((returnValue ==SQLITE_OK) ? NSLog(@"INSERT into XMLEntryID SUCCESS") : NSLog(@"INSERT into XMLEntryID Fail"));
sqlite3_step(cmp_sqlStmt1);
}
sqlite3_finalize(cmp_sqlStmt1);
sqlite3_close(database);
}
}
}else if ([p intValue] != [currentBedNo intValue]) {
}
}
}
NSLog(@"adding currentEntryID: %@", currentEntryID);
sqlite3_close(database);
}
}