内核泄漏,核心数据删除对象进程

时间:2011-01-10 12:59:05

标签: objective-c core-data

亲爱的社区。我有一个非常奇怪的泄漏,我无法通过仪器赶上。应用程序从200M到450M非常快地占用一个存储器,用于一个移除循环。对象列表的调用三列表告诉我NSManagedObjectContext NSFetchRequest->中的问题processRecentChanges-> propagatePendingDeletesAtEndOfEvent。 数据量不大,当前sqllite存储为37Mb,进程完成后内存不释放。

NSError *error = nil;
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *requestCarrier = [[NSFetchRequest alloc] init];
[requestCarrier setEntity:[NSEntityDescription entityForName:@"DestinationsListForSale"
                                      inManagedObjectContext:moc]];
[requestCarrier setPredicate:[NSPredicate predicateWithFormat:@"(carrier.name == %@)",
                              carrierName]];
NSArray *destinationsListsForSale = [moc executeFetchRequest:requestCarrier error:&error];

if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]); 
for (NSManagedObject *destinationForSale in destinationsListsForSale) [moc deleteObject:destinationForSale];


[requestCarrier release], requestCarrier = nil;
destinationsListsForSale = nil;

requestCarrier = [[NSFetchRequest alloc] init];

[requestCarrier setEntity:[NSEntityDescription entityForName:@"DestinationsListWeBuy" inManagedObjectContext:moc]];
[requestCarrier setPredicate:[NSPredicate predicateWithFormat:@"(carrier.name == %@)", carrierName]];
NSArray *destinationsListsWeBuy = [moc executeFetchRequest:requestCarrier error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]); 
for (NSManagedObject *destinationWeBuy in destinationsListsWeBuy) [moc deleteObject:destinationWeBuy];

[requestCarrier release], requestCarrier = nil;
destinationsListsWeBuy = nil;

SOLUTION v.1 重建代码以解决问题

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSManagedObject *destination in destinationsList)
{
    [moc deleteObject:destination];
    if (x % 10 == 0) {
        [moc save:&error];
        if (error) NSLog(@"Failed to save to data store removePreviousDestinationsFromMainDatabaseForCarrier: %@", [error localizedDescription]);
        [pool drain],pool = nil;
        pool = [[NSAutoreleasePool alloc] init];
    }
    x++;
}

2 个答案:

答案 0 :(得分:1)

您是否尝试按照this answer中的建议在代码中提取NSMangedObjectID

答案 1 :(得分:0)

您运行此类型的数据集是什么类型的?你确定它真的是一个泄漏,而不仅仅是处理一个非常大的数据集时正常的内存使用量增加了吗?