我的基于核心数据文档的应用程序在“另存为”时崩溃。该问题似乎类似于 cocoa-dev 线程中描述的问题,标题为“ NSPersistentDocument objects “gutted” after Duplicate, Rename in 10.9 “
主要区别在于:
- 我在 OS X 10.10 Yosemite 上定位并运行
- 使用“另存为”而不是复制
- 崩溃发生得更早。在 MOC 保存期间
这个问题甚至影响最简单的 NSPersistentDocument。它至少从 2014 年就已经存在了。因此,我希望其他人也遇到过同样的问题,并且有一个你愿意分享的解决方法。
我的示例项目使用具有单个属性的单个实体。它有一个表格视图来显示实体的所有实例和一个按钮来创建一个新的。我偏离了默认模板只是为了禁用 autosavesInPlace。
重现崩溃的步骤是:
- 在优胜美地上构建和运行。该错误似乎已在 El Capitan 中修复
- 创建一个新文档
- 插入一个新对象
- 保存文档
- 关闭文档
- 重新打开文档
- 更改表中属性的值
- 使用“另存为”以新名称保存
在 OS X Yosemite 上,这总是会因以下回溯而崩溃:
_propertyAtIndexForEntityDescription ()
snapshot_get_value_as_object ()
-[NSManagedObject(_NSInternalMethods) _validatePropertiesWithError:] ()
-[NSManagedObject(_NSInternalMethods) _validateForSave:] ()
-[NSManagedObject validateForUpdate:] ()
-[NSManagedObjectContext(_NSInternalAdditions) _validateObjects:forOperation:error:exhaustive:forSave:] ()
-[NSManagedObjectContext(_NSInternalAdditions) _validateChangesForSave:] ()
-[NSManagedObjectContext(_NSInternalChangeProcessing) _prepareForPushChanges:] ()
-[NSManagedObjectContext save:] ()
-[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] ()
-[NSDocument _writeSafelyToURL:ofType:forSaveOperation:forceTemporaryDirectory:error:] ()
-[NSDocument _writeSafelyToURL:ofType:forSaveOperation:error:] ()
-[NSDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
-[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
__66-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_22353 ()
__66-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke2350 ()
__66-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_22222 ()
__110-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:writingItemAtURL:options:error:byAccessor:]_block_invoke428 ()
-[NSFileCoordinator(NSPrivate) _invokeAccessor:orDont:andRelinquishAccessClaim:] ()
-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:writingItemAtURL:options:error:byAccessor:] ()
-[NSDocument _fileCoordinator:coordinateReadingContentsAndWritingItemAtURL:byAccessor:] ()
-[NSDocument _fileCoordinator:asynchronouslyCoordinateReadingContentsAndWritingItemAtURL:byAccessor:] ()
__66-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke2221 ()
-[NSDocument _prepareToSaveToURL:forSaveOperation:completionHandler:] ()
__66-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke ()
-[NSDocument continueFileAccessUsingBlock:] ()
-[NSDocument _performFileAccessOnMainThread:usingBlock:] ()
-[NSDocument performAsynchronousFileAccessUsingBlock:] ()
-[NSDocument saveToURL:ofType:forSaveOperation:completionHandler:] ()
__85-[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke_2 ()
-[NSDocument _commitEditingThenContinue:] ()
__62-[NSPersistentDocument _documentEditor:didCommit:withContext:]_block_invoke ()
编辑 1. 可能的解决方法:
我可以通过防止在“另存为”操作期间保存原始托管对象上下文来解决崩溃问题。“另存为”后,我立即关闭现有文档并从新位置重新打开该文档。这一切都非常难看,可能会破坏其他 NSPersistentDocument 行为。
编辑 2. 上述解决方法会丢失未保存的更改
防止保存原始托管对象上下文确实可以避免崩溃。然而,最终结果是文档的最后保存状态的副本。未保存的更改将丢失。
编辑 3. 内脏快照
当旧的托管对象上下文尝试将更改保存到新文件时,对象快照不再知道它的实体<_CDSnapshot_Entity_: 0x600001f3cfd0> (entity: (null); id: 0x40000b <x-coredata://83B64FD3-B5B9-44CB-976D-54C0326FDFF5/Entity/p1> ; data: (null))
。我没有看到任何实例变量支持-[_CDSnapshot entity]
。我认为它应该从对象 ID 中找到它。