我有一个应用程序,我导入了一堆数据,我想发生在一个后台线程。这对于少量数据来说效果很好,但现在我似乎在解析大量数据并将其导入核心数据的过程中遇到了这个错误:
Program received signal: âEXC_BAD_ACCESSâ.
下面是对后台线程的调用:
[self performSelectorInBackground:@selector(importAllData) withObject:nil];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// load manufactuers from DB
NSFetchRequest *crequest = [[NSFetchRequest alloc] init];
NSEntityDescription *manufacturer = [NSEntityDescription entityForName:@"Manufacturer" inManagedObjectContext:managedObjectContext];
[crequest setEntity:manufacturer];
NSError *cerror=nil;;
NSArray *manufacturers = [[managedObjectContext executeFetchRequest:crequest error:&cerror]mutableCopy];
[crequest release];
for (int m=0; m < [manufacturers count]; m++) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kClientListURL, [[manufacturers objectAtIndex:m]ManufacturerID]]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setNumberOfTimesToRetryOnTimeout:2];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *responseString = [request responseString];
NSArray *items = [responseString JSONValue];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
NSNumberFormatter *dec = [[NSNumberFormatter alloc]init];
[dec setNumberStyle:NSNumberFormatterDecimalStyle];
for (int i = 0; i < [items count]; i++)
{
Client *entity = (Client*) [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:managedObjectContext];
[entity setCompanyName:[[items objectAtIndex:i] objectForKey:@"CompanyName"]];
// set a bunch of other properties
[entity setManufacturer:[manufacturers objectAtIndex:m]];
statusMessage = [NSString stringWithFormat:@"importing client: %@", entity.CompanyName];
[self performSelectorOnMainThread:@selector(setStatus) withObject:nil waitUntilDone:YES];
}
[f release];
[dec release];
} else {
NSLog(@"%@",[NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]]);
}
NSError *entityerror;
if (![managedObjectContext save:&entityerror]) {
// //Handle the error.
NSLog(@"\n\n\n Error saving clients: %@ \n\n\n\n",entityerror);
}
}
//More data importing code
[pool release];
正如我所说的,对于少量的数据,这是可以正常工作的,但是现在在模拟器中,它在随机点上抛出错误的访问错误。
http://www.cocoadev.com/index.pl?DebuggingAutorelease
如果对象在NSAutoReleasePool中,是否不需要释放它们?我是不是让他们在某个时候被释放了两次?
如果我删除了发布声明,那么在使用Command-Shift-A构建时会出现潜在的泄漏错误?