使用iPad上的Core Data导入数据时出现内存不足问题

时间:2010-12-13 19:35:22

标签: iphone objective-c ipad core-data

我在数据导入时遇到了一些内存问题,我从Web服务中提取了一些xml并将其保存在核心数据中。

这是我的代码:

for (int m=0; m < [manufacturers count]; m++) {

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kClientListURLv2, [[manufacturers objectAtIndex:m]ManufacturerID]]];
    NSLog(@"getting data:%@", url) ;
    NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
    int j = 0;
    for (NSDictionary* dict in array) {
        j=j+1;
        Client *entity = (Client*) [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:managedObjectContext];
        //[entity setValuesForKeysWithDictionary:dict];
        [entity setBillingState:[dict valueForKey:@"BillingState"]];
        [entity setBillingCity:[dict valueForKey:@"BillingCity"]];
        [entity setBillingCountry:[dict valueForKey:@"BillingCountry"]];
        [entity setCurrentBalance:[dict valueForKey:@"CurrentBalance"]];
        [entity setArOver90:[dict valueForKey:@"ArOver90"]];
        [entity setPassword:[dict valueForKey:@"Password"]];
        [entity setPricingDiscount:[dict valueForKey:@"PricingDiscount"]];
        [entity setEmail:[dict valueForKey:@"Email"]];
        [entity setCompanyName:[dict valueForKey:@"CompanyName"]];
        [entity setClientID:[dict valueForKey:@"ClientID"]];
        [entity setAr6190:[dict valueForKey:@"Ar6190"]];
        [entity setBillingAddress2:[dict valueForKey:@"BillingAddress2"]];
        [entity setLastName:[dict valueForKey:@"LastName"]];
        [entity setContactName:[dict valueForKey:@"ContactName"]];
        [entity setAr3160:[dict valueForKey:@"Ar3160"]];
        [entity setSalesRepID:[dict valueForKey:@"SalesRepID"]];
        [entity setShippingAddress2:[dict valueForKey:@"ShippingAddress2"]];
        [entity setShippingCity:[dict valueForKey:@"ShippingCity"]];
        [entity setManufacturerID:[dict valueForKey:@"ManufacturerID"]];
        [entity setShippingState:[dict valueForKey:@"ShippingState"]];
        [entity setAr30:[dict valueForKey:@"Ar30"]];
        [entity setBillingZip:[dict valueForKey:@"BillingZip"]];
        [entity setShippingAddress:[dict valueForKey:@"ShippingAddress"]];
        [entity setShippingZip:[dict valueForKey:@"ShippingZip"]];
        [entity setURL:[dict valueForKey:@"URL"]];
        [entity setPhone:[dict valueForKey:@"Phone"]];
        [entity setCreditLimit:[dict valueForKey:@"CreditLimit"]];
        [entity setClientStatus:[dict valueForKey:@"ClientStatus"]];
        [entity setFax:[dict valueForKey:@"Fax"]];
        [entity setIsDeleted:[dict valueForKey:@"IsDeleted"]];
        [entity setShippingCountry:[dict valueForKey:@"ShippingCountry"]];
        [entity setClientNumber:[dict valueForKey:@"ClientNumber"]];
        [entity setBillingAddress:[dict valueForKey:@"BillingAddress"]];
        [entity setUsername:[dict valueForKey:@"Username"]];
        [entity setIsAddedToServer:[dict valueForKey:@"IsAddedToServer"]];
        [entity setFirstName:[dict valueForKey:@"FirstName"]];
        [entity setRepNumber:[dict valueForKey:@"RepNumber"]];
        [entity setManufacturer:[manufacturers objectAtIndex:m]];
        [entity setIsAddedToServer:[NSNumber numberWithInt:1]];

        for (NSDictionary* dict2 in [dict valueForKey:@"ShippingAddresses"]) {

            if ([[NSString stringWithFormat:@"%@", [dict valueForKey:@"ShippingAddresses"]] length] > 1) {
                ShippingAddress *taggedItem = (ShippingAddress *) [NSEntityDescription insertNewObjectForEntityForName:@"ShippingAddress" inManagedObjectContext:managedObjectContext];
                [taggedItem setValuesForKeysWithDictionary:dict2];
                [taggedItem setClient: entity];

                NSError *error;
                if (![managedObjectContext save:&error]) {
                    NSLog(@"Save Error: %@",error);
                }
            }   
        }

        if (j%saveThreshold==0) {
            NSLog(@"Saving after 500 items");
            NSError *error;
            if (![managedObjectContext save:&error]) {
                NSLog(@"Save Error: %@",error);
            }
        }
    }

    [array release];
    NSError *entityerror;
    if (![managedObjectContext save:&entityerror]) {
        //  //Handle the error.
        NSLog(@"\n\n\n Error saving: %@ \n\n\n\n",entityerror);
    }
}

基本上,这是循环并创建我的所有NSManagedObjects,然后在每500个后保存上下文。

代码工作很好而且非常快但内存增长,如果客户端下载它也会因为内存不足而导致应用程序崩溃。是否有某种类型的释放或我可以在managedObjectContext上做些什么 - 在这里抓住吸管。

1 个答案:

答案 0 :(得分:2)

您可以尝试将孔码放在

for (int m=0; m < [manufacturers count]; m++) {
}

在一个单独的方法中并加上一些

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[pool drain];
围绕着它。这应该完全释放任何NSArray *array。 不要问我为什么在release上没有删除它,但几周前我遇到了同样的问题。

您可以使用Performance Tools运行项目,尤其是对象分配和泄漏。 它会告诉你,在那个时候使用了多少内存。