[英]CoreData inserting very huge DB exc_bad_access
I have ~60000 values in my DB. 我的数据库中有〜60000个值。 I must to keep them all.
我必须保留所有这些。 But while inserting values I get exc_bad_access.
但是在插入值时,我得到了exc_bad_access。 Here is my code:
这是我的代码:
if (context == nil)
{
context = [(radioAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", context);
}
[self performSelectorOnMainThread:@selector(deleteAllFromDB) withObject:nil waitUntilDone:YES];
// here is JSON parsing
int i=0;
@synchronized(self)
{
NSLog(@"Start");
NSEntityDescription *entity = [[NSEntityDescription entityForName:@"Station" inManagedObjectContext:context] autorelease];
for (NSString*string in stations)
{
if (![string isEqualToString:@""])
{
Station*station = [[[Station alloc] initWithEntity:entity insertIntoManagedObjectContext:nil] retain];
NSError *err = nil;
id parsedData = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers error:&err];
if (err) {
NSLog(@"error is %@", [err localizedDescription]);
}
[station setName:[parsedData objectForKey:@"nm"]];
[station setBit: [parsedData objectForKey:@"btr"]];
[station setEnabled: [NSNumber numberWithInt:1]];
//encoding id of the station
scanner = [NSScanner scannerWithString:[parsedData objectForKey:@"id"]];
[scanner scanHexInt:&tempInt];
NSNumber *numb = [[NSNumber alloc]initWithInt:i];
[station setNumber: [NSNumber numberWithInt:[numb intValue]]];
//encoding country ID
numb = [NSNumber numberWithInt:i];
[station setCountryID: [NSNumber numberWithInt:[numb intValue]]];
//encoding genre ID
numb = [NSNumber numberWithInt:i];
[station setGenerID:[NSNumber numberWithInt:[numb intValue]]];
[station setOrder:[NSNumber numberWithInt:i]];
[context insertObject:station];
float k = [stations count];
k = (i + 1)/k;
[self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
i++;
[scanner release];
[numb release];
[station release];
[parsedData release];
}
}
[context save:nil];
[entity release];
NSLog(@"Stop");
NSEntityDescription *entityGen = [NSEntityDescription entityForName:@"Genre" inManagedObjectContext:context];
NSURL* urlForGenre = [NSURL URLWithString:@"xxx.xxx"];
NSData *genres = [[NSData alloc] initWithContentsOfURL:urlForGenre];
NSError *err = nil;
NSArray *parsedGenres = [NSJSONSerialization JSONObjectWithData:genres options:NSJSONReadingMutableContainers error:&err];
if (err)
{
NSLog(@"error is %@", [err localizedDescription]);
}
for (int i =0; i<parsedGenres.count; i++)
{
Genre*gen = [[Genre alloc] initWithEntity:entityGen insertIntoManagedObjectContext:nil];
gen.name = [[parsedGenres objectAtIndex:i] objectForKey:@"nm"];
int tempInt;
NSScanner *scanner= [[NSScanner alloc] init];
scanner = [NSScanner scannerWithString:[[parsedGenres objectAtIndex:i] objectForKey:@"id"]];
[scanner scanInt:&tempInt];
NSNumber *numb = [[NSNumber alloc] init];
numb = [NSNumber numberWithInt:tempInt];
gen.number = [NSNumber numberWithInt:[numb integerValue]];
float k = [parsedGenres count];
k = (i + 1)/k;
[self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
[context insertObject:gen];
[gen release];
}
[entityGen release];
[context save:nil];
NSEntityDescription *entityCon = [NSEntityDescription entityForName:@"Country" inManagedObjectContext:context];
NSURL* urlForCoun = [NSURL URLWithString:@"yyy.yyy"];
NSData *countr = [[NSData alloc] initWithContentsOfURL:urlForCoun];
err = nil;
NSArray *parsedCountr = [NSJSONSerialization JSONObjectWithData:countr options:NSJSONReadingMutableContainers error:&err];
if (err)
{
NSLog(@"error is %@", [err localizedDescription]);
}
NSMutableArray* temp = [[NSMutableArray alloc] init];
for (int i =0; i<parsedCountr.count; i++)
{
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
countr.name = [[parsedCountr objectAtIndex:i] objectForKey:@"nm"];
int tempInt;
NSScanner *scanner= [[NSScanner alloc] init];
scanner = [NSScanner scannerWithString:[[parsedCountr objectAtIndex:i] objectForKey:@"id"]];
[scanner scanInt:&tempInt];
NSNumber *numb = [[NSNumber alloc] init];
numb = [NSNumber numberWithInt:tempInt];
countr.number = [NSNumber numberWithInt:[numb integerValue]];
[temp addObject:countr];
float k = [parsedCountr count];
k = (i + 1)/k;
[self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
[context insertObject:countr];
[countr release];
}
[context save:nil];
If my app doesn't crash, yes, it happens not by every launch. 如果我的应用程序没有崩溃,是的,并非每次启动都会发生。 It crashes on:
崩溃:
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
Please help! 请帮忙! And sorry for my terrible English.
对不起,我的英语不好。
Update: 更新:
On: Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil]; 开启:Country * countr = [counter initWithEntity:entityCon insertIntoManagedObjectContext:nil];
Thread 7: EXC_BAD_ACCESS (code=1, address=0x3f800008)
线程7:EXC_BAD_ACCESS(代码= 1,地址= 0x3f800008)
Other error is exc_bad_access too. 其他错误也是exc_bad_access。 But with code=2 and on other thread.
但是在code = 2和其他线程上。
Update 2: 更新2:
I enabled zombies mode in scheme of my debug. 我在调试方案中启用了僵尸模式。 Nothing happened.
没啥事儿。
Update 3: 更新3:
I think I have problems with memory. 我想我的记忆有问题。
GuardMalloc: Failed to VM allocate 4128 bytes
GuardMalloc:无法为VM分配4128字节
GuardMalloc: Explicitly trapping into debugger!!!
GuardMalloc:明确陷入调试器!!!
You can't pass nil
as managedObjectContext
parameter to initWithEntity:insertIntoManagedObjectContext:
method. 您不能将
nil
作为managedObjectContext
参数传递给initWithEntity:insertIntoManagedObjectContext:
方法。 Try changing this: 尝试更改此:
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
to this 对此
// pass managed object context parameter
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:context];
Based on the documentation and my experience you can pass nil as managedObjectContext. 根据文档和我的经验,您可以将nil作为managedObjectContext传递。
I was also getting an EXC_BAD_ACCESS and it came that the reason was that I was not retaining the managed object model(I was only creating a model to get the entity description and then it was released). 我也得到了EXC_BAD_ACCESS,原因是我没有保留受管对象模型(我只是创建一个模型来获取实体描述,然后将其发布)。
In particular, this is what I was doing: 特别是,这就是我正在做的:
In a test class, in the setUp method: 在测试类的setUp方法中:
- (void)setUp
{
[super setUp];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *modelURL = [bundle URLForResource:@"TestDatamodel" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
STAssertNotNil(model, nil);
_entity = [[model entitiesByName] objectForKey:NSStringFromClass([MyEntity class])];
STAssertNotNil(_entity, nil);
// ...
}
Then in a test method I was creating a managed object without a context like this: 然后在测试方法中,我创建了一个没有这样的上下文的托管对象:
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:_entity insertIntoManagedObjectContext:nil];
And I was getting EXC_BAD_ACCESS here. 我在这里得到EXC_BAD_ACCESS。
The solution was to add an ivar to the test class to keep a reference to the managed object model. 解决方案是在测试类中添加一个ivar,以保留对托管对象模型的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.