0

我的数据库中有大约 60000 个值。我必须保留它们。但是在插入值时,我得到了 exc_bad_access。这是我的代码:

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];

如果我的应用程序没有崩溃,是的,它不会在每次启动时发生。它崩溃:

Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];

请帮忙!对不起我糟糕的英语。

更新:

开启:Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];

线程 7:EXC_BAD_ACCESS(代码=1,地址=0x3f800008)

其他错误也是 exc_bad_access。但是使用 code=2 和其他线程。

更新 2:

我在调试方案中启用了僵尸模式。没啥事儿。

更新 3:

我觉得我的记忆有问题。

GuardMalloc:VM 分配 4128 字节失败

GuardMalloc:显式陷入调试器!!!

4

2 回答 2

0

您不能nil作为managedObjectContext参数传递给initWithEntity:insertIntoManagedObjectContext:方法。尝试改变这个:

Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];

对此

// pass managed object context parameter
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:context];
于 2013-02-07T17:12:54.460 回答
0

根据文档和我的经验,您可以将 nil 作为 managedObjectContext 传递。

我也得到了一个 EXC_BAD_ACCESS,原因是我没有保留托管对象模型(我只是创建一个模型来获取实体描述,然后它被释放)。

特别是,这就是我正在做的事情:

在测试类中,在 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);
    // ...
}

然后在一个测试方法中,我创建了一个没有这样上下文的托管对象:

NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:_entity insertIntoManagedObjectContext:nil];

我在这里得到了 EXC_BAD_ACCESS。

解决方案是向测试类添加一个 ivar 以保留对托管对象模型的引用。

于 2013-10-23T15:04:41.270 回答