Tro*_*sen 6 macos cocoa core-data objective-c nsoutlineview
我运行我的程序,创建使用NSTreeController在NSOutlineView中显示的Core Data内容.第二次运行程序时,我想清理我的内容,然后运行下面粘贴的方法.该方法要么在完成之前挂起很长时间(600秒),要么崩溃.如果我的实体(500-1000)很少,那么与我有很多(200,000)个实体传递此方法所需的时间相比,如果它完全通过则需要更少的时间.我需要知道的是,在重新运行程序并重新填写之前,是否有更好的方法来清除/刷新/重置我的内容以清除我的内容.具体来说,我希望我能够快速响应我的内容更改,并且我需要驱动我的核心数据内容才能重置.NSTreeController
NStreeController
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");
}];
});
}
Run Code Online (Sandbox Code Playgroud)
managedobjectcontext(context
)作为类型运行,NSMainQueueConcurrencyType
方法在主线程上运行.重点建议或重置/刷新NSOutlineView +核心数据组合的有用示例将不胜感激.谢谢.干杯,特隆德
为了回应@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
继续.
当我重新运行该应用程序时,processPendingChanges
注释掉它仍然悬挂.
更新
我相信我已经解决了这个问题,但我对于为什么会这样做有点不确定.似乎我的第一个方法进入了一个无法释放其对象的无限循环.以下简单的解决方案有效:
__weak __typeof__(self) weakSelf = self;
dispatch_sync(dispatch_get_main_queue(), ^{
[weakSelf.coreDataController._coreDataHelper.context reset];
});
Run Code Online (Sandbox Code Playgroud)
我确信要正确清空托管对象上下文,我必须单独删除每个实体.重置功能似乎相当暴力,它实际上是否清理内存并确保一切正常?如果有人想对此有所了解,那将是值得赞赏的.
再看一遍,您获取了某种类型的所有对象performBlockAndWait
——这会阻塞主线程,因为您有 mainQueueConcurrency 并且使用了 PerformBlock 的 andWait 版本。
然后,您可以一一删除每个对象。这些对象位于带有附加大纲视图的树形数据结构中(请参阅堆栈跟踪中的 KVO 消息)。这些对象具有需要由核心数据维护的多对多关系,地狱,你甚至可以有级联删除规则。(请参阅堆栈跟踪中的propagateDelete 和maintainInverseRelationship)无论如何,您都会开始请求数据源和视图开始在主线程上执行大量工作。如果您想在后台迭代所有对象,您可以尝试使用带有 privateQueueConcurrency 的子 MOC。
但是,就像评论指出的那样:
NSManagedObjectContextreset
绝对可以释放内存,并且它非常适合您在这里要做的事情:清除所有内容。
不过,这引出了一个问题:为什么您首先从磁盘上的存储加载模型。
如果您需要核心数据,但不需要在运行程序之间保持持久性,则可以使用存储来初始化 persistenceStoreCoordinator,NSInMemoryStoreType
而不是将其指向文件 URL。
归档时间: |
|
查看次数: |
301 次 |
最近记录: |