核心数据:失去了保存新文档的能力,但仍然可以编辑和保存现有文档

Core Data: Lost the ability to save new documents, but still can edit and save existing documents

我目前正在使用 NSPersistentDocument 和轻量级迁移开发应用程序的第一个版本。数据模型现在升级到版本 12。从版本 9 切换到版本 10 时,我失去了保存新文档的能力,但直到现在我才注意到,因为我正在测试与第一个版本相同的旧文档。

我仍然可以很好地编辑和保存这些文档,我什至可以使用我的应用程序的旧版本生成文档并使用当前版本迁移它们。

当我打开一个新文档,编辑它并等待自动保存或自己保存时,configurePersistentStoreCoordinatorForURL 崩溃:

2017-03-31 07:58:51.409160+0200 MyCalcApp[5172:1857474] [General] Cannot perform operation since entity with name '(null)' cannot be found
2017-03-31 07:58:51.411774+0200 MyCalcApp[5172:1857474] [General] (
    0   CoreFoundation                      0x00007fffd037937b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffe516d48d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffd03f7c3d +[NSException raise:format:] + 205
    3   AppKit                              0x00007fffce48ff1b -[_NSManagedProxy _entity] + 138
    4   AppKit                              0x00007fffce490178 -[_NSManagedProxy fetchRequestWithSortDescriptors:limit:] + 89
    5   AppKit                              0x00007fffce49063f -[_NSManagedProxy _storesDidChange:] + 119
    6   CoreFoundation                      0x00007fffd030550c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    7   CoreFoundation                      0x00007fffd030540b _CFXRegistrationPost + 427
    8   CoreFoundation                      0x00007fffd0305172 ___CFXNotificationPost_block_invoke + 50
    9   CoreFoundation                      0x00007fffd02c25a3 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1827
    10  CoreFoundation                      0x00007fffd02c15dc _CFXNotificationPost + 604
    11  Foundation                          0x00007fffd1ce5997 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
    12  CoreData                            0x00007fffcfed9736 -[NSPersistentStoreCoordinator(_NSInternalMethods) _postStoresChangedNotificationsForStores:changeKey:options:] + 246
    13  CoreData                            0x00007fffcff9e1c7 __91-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]_block_invoke + 3015
    14  CoreData                            0x00007fffcffaf378 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 200
    15  libdispatch.dylib                   0x00000001000da78c _dispatch_client_callout + 8
    16  libdispatch.dylib                   0x00000001000db5ad _dispatch_barrier_sync_f_invoke + 307
    17  CoreData                            0x00007fffcff99f52 _perform + 210
    18  CoreData                            0x00007fffcfec9994 -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 564
    19  AppKit                              0x00007fffce4b3c54 -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:] + 1176
    20  MyCalcApp                      0x000000010000ee1f -[Document configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:] + 735
    21  AppKit                              0x00007fffce7d8ee8 -[NSPersistentDocument(NSDeprecatedInternal) _configurePersistentStoreCoordinatorForURL:ofType:error:] + 159
    22  AppKit                              0x00007fffce4b43b9 -[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] + 754
    23  AppKit                              0x00007fffce4b5ad0 -[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] + 767
    24  AppKit                              0x00007fffce930935 __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_2.1116 + 233
    25  AppKit                              0x00007fffce93083d __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke.1113 + 454
    26  AppKit                              0x00007fffce92e973 __85-[NSDocument(NSDocumentSaving) _saveToURL:ofType:forSaveOperation:completionHandler:]_block_invoke_2.969 + 2071
    27  AppKit                              0x00007fffce34916b __62-[NSDocumentController(NSInternal) _onMainThreadInvokeWorker:]_block_invoke.1972 + 160
    28  CoreFoundation                      0x00007fffd030f21c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    29  CoreFoundation                      0x00007fffd02f0034 __CFRunLoopDoBlocks + 356
    30  CoreFoundation                      0x00007fffd02efb76 __CFRunLoopRun + 1878
    31  CoreFoundation                      0x00007fffd02ef1c4 CFRunLoopRunSpecific + 420
    32  HIToolbox                           0x00007fffcf850ebc RunCurrentEventLoopInMode + 240
    33  HIToolbox                           0x00007fffcf850cf1 ReceiveNextEventCommon + 432
    34  HIToolbox                           0x00007fffcf850b26 _BlockUntilNextEventMatchingListInModeWithFilter + 71
    35  AppKit                              0x00007fffcddebe24 _DPSNextEvent + 1120
    36  AppKit                              0x00007fffce56785e -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
    37  AppKit                              0x00007fffcdde07ab -[NSApplication run] + 926
    38  AppKit                              0x00007fffcddab1de NSApplicationMain + 1237
    39  MyCalcApp                      0x0000000100017ac2 main + 34
    40  libdyld.dylib                       0x00007fffe5a52235 start + 1
    41  ???                                 0x0000000000000005 0x0 + 5
)

我寻找了一个缺少名称的实体,但没有找到。数据模型看起来不错,即使我查看 XML 源代码也是如此。没有缺少实体名称的 NSObjectControllerNSArrayControllerconfigurePersistentStoreCoordinatorForURL 无论我是否覆盖它都会崩溃(对于轻量级迁移)。

我清除了所有我能想到的 Xcode 派生数据文件夹,在不同 Xcode 版本(8.3 和 8.2.1)的不同 Mac 上编译和 运行 应用程序,到处都是同样的问题。 运行 参数 com.apple.CoreData.SQLDebug 没有提供任何有用的信息。

我从模型版本 9 到 12 添加了一些实体,我可能删除了一个,但我记不清了。

是否有其他地方Xcode隐藏数据模型版本信息,例如散列?任何想法在哪里看?我应该从头开始重新创建数据模型吗?

向其他人解释您的问题以解决问题总是好的。我发现了我没想到的错误。

我有两个通过一对多关系连接的实体:一系列测量和测量。我将它们列在两个 table 视图中:

如果我在左侧 table 视图中单击一个系列,我希望右侧 table 视图显示相应的测量值。因此,左侧 table 视图引用实体名称模式中的数组控制器,其托管对象上下文绑定到 File's Owner.managedObjectContext。为了使右侧 table 视图中的值取决于左侧的选择,我在 class 模式(NSMutableDictionary)中创建了一个 NSArrayController 并将其内容集绑定到正确的关系集第一个控制器:

我做错的是我将该控制器的托管对象上下文也绑定到 File's Owner.managedObjectContext。我认为这是使任何更改永久化所必需的,但事实并非如此。奇怪的是,它在我使用现有文档时有效,但在我创建新文档时却没有。所以这些是测量的正确设置 NSArrayController: