CoreData & importing a large amount of data
CoreData & importing a large amount of data
- Subject: CoreData & importing a large amount of data
- From: Dominik Paulmichl <email@hidden>
- Date: Wed, 19 Oct 2005 20:21:56 +0200
Hi all,
Here my problem:
I have a CoreData Enity which hold all the german bank account data. It
has only two attributes blz and name.
The german bank account numbers are updated about three times a year.
Well, i read the topics I found in the cocoabuilder list, but the
gathered information is not clear for me.
For testing and development purposes I use an XML data store. So I know
that Core Data makes in memory searches.
Even when I save each new entry the Mac ran very fast out of memory. :-(
How can I avoid this??
Here my source code:
-(void)doBlzImport:(NSString*)thePathToFile
{
int i = 0;
int lineLength = 0;
int anzFound = 0;
id aBank = nil;
NSString* theLine = nil;
NSString* theBlz = nil;
NSString* existBlz = nil;
NSString* existBName = nil;
NSString* thePredStr = nil;
NSString* theBankName = nil;
NSString* impString = nil;
NSManagedObject *newBank = nil;
NSPredicate *predicate = nil;
NSError *error;
// open the blz file and split into lines
NSString* theContent = [[NSString alloc]
initWithContentsOfFile:thePathToFile];
NSArray* theLines = [theContent componentsSeparatedByString:@"\n"];
int anz = [theLines count];
// Setup the panel
[theImpPanel setTitle:@"Importing BLZ"];
[theImpPanel makeKeyAndOrderFront:self];
[theProgrIndi setMaxValue:anz];
// disable undo manager
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// walking through each line
for(i = 0; i < anz; i++)
{
impString = [NSString stringWithFormat:@"Importing Item %d of %d",
i+1, anz];
[theProgressText setStringValue:impString];
[theProgressText displayIfNeeded];
theLine = [theLines objectAtIndex:i];
lineLength = [theLine length];
if(lineLength == 189)
{
// only main bank accounts should be imported
if([theLine characterAtIndex:8] == '1')
{
// create the desired substrings
theBlz = [theLine substringWithRange:NSMakeRange(0, 8)];
theBankName = [theLine substringWithRange:NSMakeRange(27, 58)];
// create a predicate to check for exisrtance
thePredStr = [NSString stringWithFormat:@"blz == %@", theBlz];
predicate = [NSPredicate predicateWithFormat:thePredStr];
[theBankArrCrtl setFilterPredicate:predicate];
//[theBankArrCrtl fetch:self];
anzFound = [[theBankArrCrtl arrangedObjects] count];
if(anzFound == 0)
{
// nothing found create a new entry / record
newBank = [NSEntityDescription
insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:[self
managedObjectContext]];
[newBank setValue:[NSNumber numberWithInt:[theBlz intValue]]
forKey:@"blz"];
[newBank setValue:theBankName forKey:@"name"];
if(![[self managedObjectContext] save:&error])
NSLog(@"Error while saving blz import: %d", i);
else
[newBank release];
}// if
else
{
// entry found update the existing one
aBank = [[theBankArrCrtl arrangedObjects] objectAtIndex:0];
existBlz = [[aBank valueForKey:@"blz"] stringValue];
existBName = [aBank valueForKey:@"name"];
if(![existBlz isEqualToString:theBlz])
[aBank setValue:[NSNumber numberWithInt:[theBlz intValue]]
forKey:@"blz"];
if(![existBName isEqualToString:theBankName])
[aBank setValue:theBankName forKey:@"name"];
// release unneeded stuff
[existBlz release];
[existBName release];
[aBank release];
}// else
// release unneeded stuff
[predicate release];
[thePredStr release];
[theBlz release];
[theBankName release];
[theBankArrCrtl setFilterPredicate:nil];
}// if
}// if
// update the progress indicator and force redraw
[theProgrIndi incrementBy:1];
[theProgrIndi displayIfNeeded];
[theLine release];
}// for
// done, release all other stuff
[theContent release];
[theImpPanel close];
// enable the undo manager again
[[[self managedObjectContext] undoManager] enableUndoRegistration];
}
Have I missed or overlooked something??
Thanks in advance for any hints!
Regards
Dominik
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden