gpt4 book ai didi

objective-c - 重置核心数据驱动的 treeController 内容

转载 作者:太空狗 更新时间:2023-10-30 03:45:28 26 4
gpt4 key购买 nike

我运行我的程序来创建显示在 NSOutlineView 中的核心数据内容使用 NSTreeController .第二次运行我的程序时,我想清理我的 NSTreeController 的内容。我运行下面粘贴的方法。该方法要么在完成之前挂起很长时间(600 秒),要么崩溃。如果我的 NStreeController 中的实体很少 (500-1000)与我有很多(200,000)个实体通过此方法(如果它完全通过)相比,它花费的时间要少得多。我需要知道的是是否有更好的方法来清除/刷新/重置我的 NStreeController 的内容清除我的 NSoutlineView在我重新运行我的程序并填写 NStreeController 之前再次。具体来说,我想要我的 NSOutlineView快速响应我的 NSTreeController 内容的变化,我需要我的核心数据驱动的内容 NSTreeController能够被重置。

-(void) cleanSDRDFileObjects
{
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.outlineView collapseItem:nil collapseChildren:YES];
[weakSelf.coreDataController._coreDataHelper.context performBlockAndWait:^{

NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"SDRDFileObject" inManagedObjectContext:weakSelf.coreDataController._coreDataHelper.context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];

NSArray * result = [weakSelf.coreDataController._coreDataHelper.context executeFetchRequest:request error:nil];
for (id fileobject in result){
[weakSelf.coreDataController._coreDataHelper.context deleteObject:fileobject];
}
[weakSelf.coreDataController._coreDataHelper.context processPendingChanges];

NSLog(@"Finished deleting all objects");
}];
});
}

managedobjectcontext ( context ) 作为 NSMainQueueConcurrencyType 类型运行并且该方法在主线程上运行。非常感谢关于重置/刷新 NSOutlineView + Core Data 组合的改进建议或有用示例。谢谢。干杯,特隆

为了回答@TomHarringtons 的问题,我拍了一张我的 Time Profiler 的照片。我真的不t understand why it hangs on this method, however, after commenting this methods out (```processPendingChanges```), it still hangs and takes forever to finish (6 minutes). It seems the process gets stuck on the main thread and can不继续了。

enter image description here

当我重新运行应用程序时,使用 processPendingChanges注释掉它仍然挂起。

enter image description here

更新

我相信我已经解决了这个问题,但我不太确定为什么会这样。似乎我的第一个方法进入了一个不释放其对象的无限循环。以下简单的解决方案有效:

     __weak __typeof__(self) weakSelf = self;
dispatch_sync(dispatch_get_main_queue(), ^{
[weakSelf.coreDataController._coreDataHelper.context reset];
});

我确信要正确清空托管对象上下文,我必须单独删除每个实体。重置功能看起来很蛮力,它是否真的清理内存并确保一切正常?如果有人想阐明这一点,我们将不胜感激。

最佳答案

再看一遍,您在 performBlockAndWait 中获取了一个类型的所有对象——这会阻塞主线程,因为您有 mainQueueConcurrency 并且您使用了 performBlock 的 andWait 版本。

然后您一个一个地删除每个对象。这些对象位于一个树数据结构中,并附加了一个大纲 View (请参阅堆栈跟踪中的 KVO 消息)。这些对象具有需要由核心数据维护的对多关系, hell ,你甚至可以有级联删除规则。 (请参阅堆栈跟踪中的 propagateDelete 和 maintainInverseRelationship)无论如何,您开始请求数据源和 View 开始在主线程上做大量工作。如果您想在后台迭代所有对象,您可以尝试使用带有 privateQueueConcurrency 的子 MOC。

但是,就像评论指出的那样:

NSManagedObjectContext 的reset 绝对可以释放内存,这对于您想在这里做的事情来说很好:把所有东西都扔掉。

不过,这引出了一个问题,为什么首先要从磁盘上的商店加载模型。

如果你想要 Core Data,而不是你运行程序之间的持久性,你可以用 NSInMemoryStoreType 的存储初始化 persistentStoreCoordinator,而不是将它指向一个文件 URL。

关于objective-c - 重置核心数据驱动的 treeController 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com