Search code examples
objective-cmacoscore-datacore-data-migrationnspersistentdocument

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


I am currently developing the first version of an application using NSPersistentDocument and lightweight migration. The data model went up to version 12 now. When switching from version 9 to version 10, I lost the ability to save new documents, but I didn't notice until now since I was testing with the same old documents from the first versions.

I can still edit and save these documents just fine, I can even produce documents with older versions of my application and migrate them with the current version.

When I open a new document, edit it and wait for auto save or save it myself, configurePersistentStoreCoordinatorForURL crashes:

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
)

I looked for an entity with a missing name but did not find one. The data model looks fine, even when I look at the XML source code. There is no NSObjectController or NSArrayController which misses its entity name. configurePersistentStoreCoordinatorForURL crashes whether or not I overwrite it (for lightweight migration).

I cleared all the Xcode derived data folders I could think of, compiled and ran the app on different Macs with different Xcode versions (8.3 and 8.2.1), same problem everywhere. The run argument com.apple.CoreData.SQLDebug does not give any helpful information.

I added some entities from model version 9 to 12 and I might have deleted one, but I can't remember exactly.

Is there some other place Xcode is hiding data model version information such as hashes? Any ideas where to look? Shall I recreate the data model from scratch?


Solution

  • It is always good to explain your problem to somebody else in order to solve it. I found the bug where I didn't expect it.

    I have two entities that are connected by a to-many relationship: series of measurements and measurements. I list them in two table views:

    Two table views

    If I click on a series in the left table view, I want the right table view to show the corresponding measurements. So the left table view refers to an array controller in Entity Name mode and with its Managed Object Context bound to File's Owner.managedObjectContext. To make the values in the right table view depend on the selection on the left, I created an NSArrayController in class mode (NSMutableDictionary) and bound its content set to the correct relationship set of the first controller:

    array controller mode wrong bindings

    What I did wrong was that I bound that controller's Managed Object Context to File's Owner.managedObjectContext, too. I thought that was needed to make any changes permanent, but it is not. Strangely it worked when I was using existing documents, but it didn't when I created new ones. So these are the correct settings for the Measurements NSArrayController:

    correct bindings