简体   繁体   中英

error NSObjectController invalid reference to NSManagedObjectContext addObject

Question:

I seem to have bindings set without design time or build errors, but at run-time an instance of a an NSManagedObject (an Account entity) isn't found. What is my configuration mistake?

Configuration:

This is a basic intro project. I've got a "no-code" solution by using XCode to auto generate most of an MVC application (not for iOS). Xcode generated an App Delegate with NSManagedObjectContext and a MainMenu.xib. To the xib, I added an NSObjectController, a Button used to create a new entity and a TextField to edit a value on the instance of the newly created entity. I created the data model, had XCode generate NSManagedObjects for my entities, and now I'm simply setting the bindings to connect the MVC.

Conceptual binding flows:

XIB's Button -> XIB's NSObjectController add: -> AppDelegate's managedObjectContext
AppDelegate's managedObjectContext -> NSObjectController's selection.name -> XIB's TextField

Button settings

Bindings: Sent Actions = add: --> ObjectController

ObjectController settings

Attributes: mode = Entity Name, value = Account, prepares content = true, Editable = true
Bindings; Parameters: bind to App Delegate, controller key = null, model key path = managedObjectContext

TextField settings

Bindings: Bind To = Object Controller, Controller Key = selection, Model Key Path = name

Problem:

There are no errors at design time or during build, and the run-time error log is below. I get the error when the XIB loads before I click the button. I get an identical error after I click the button. When the XIB loads, I'm guessing that the TextField is trying to fetch the column on a null entity. When I click the button, I'm guessing that the TextField doesn't have a handle to the instance that was created.

Error Log

2014-05-28 16:09:11.526 BingoAppServer[2060:303] Cannot perform operation since entity with name 'Account' cannot be found
2014-05-28 16:09:11.529 BingoAppServer[2060:303] (
0   CoreFoundation                      0x00007fff933da25c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff98919e75 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff933da10c +[NSException raise:format:] + 204
3   AppKit                              0x00007fff97020658 -[_NSManagedProxy _entity] + 141
4   AppKit                              0x00007fff97020515 -[_NSManagedProxy fetchRequestWithSortDescriptors:limit:] + 95
5   AppKit                              0x00007fff972708fb -[NSObjectController(NSManagedController) _executeFetch:didCommitSuccessfully:actionSender:] + 73
6   AppKit                              0x00007fff9744b846 _NSSendCommitEditingSelector + 267
7   AppKit                              0x00007fff970f4f78 -[NSController _controllerEditor:didCommit:contextInfo:] + 182
8   CoreFoundation                      0x00007fff932c5a5c __invoking___ + 140
9   CoreFoundation                      0x00007fff932c58c4 -[NSInvocation invoke] + 308
10  CoreFoundation                      0x00007fff93368516 -[NSInvocation invokeWithTarget:] + 54
11  Foundation                          0x00007fff934decb7 __NSFireDelayedPerform + 333
12  CoreFoundation                      0x00007fff93341494 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
13  CoreFoundation                      0x00007fff93340fcf __CFRunLoopDoTimer + 1151
14  CoreFoundation                      0x00007fff933b25aa __CFRunLoopDoTimers + 298
15  CoreFoundation                      0x00007fff932fc755 __CFRunLoopRun + 1525
16  CoreFoundation                      0x00007fff932fbf25 CFRunLoopRunSpecific + 309
17  HIToolbox                           0x00007fff8c772a0d RunCurrentEventLoopInMode + 226
18  HIToolbox                           0x00007fff8c7727b7 ReceiveNextEventCommon + 479
19  HIToolbox                           0x00007fff8c7725bc _BlockUntilNextEventMatchingListInModeWithFilter + 65
20  AppKit                              0x00007fff96cb326e _DPSNextEvent + 1434
21  AppKit                              0x00007fff96cb28bb -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
22  AppKit                              0x00007fff96ca69bc -[NSApplication run] + 553
23  AppKit                              0x00007fff96c917a3 NSApplicationMain + 940
24  BingoAppServer                      0x0000000100001252 main + 34
25  libdyld.dylib                       0x00007fff915ec5fd start + 1
)

It's not saying it can't find this Account that you want — it's telling you it can't find the Account entity description in your managed object model at all. Check to ensure that you're using the right managed object model and that it has an entity named Account.

Your objectController 's content shouldn't be bound to a managedObjectContext . It looks like what's happening is your object controller doesn't have a managed object context set, which is why the initial fetch is failing.

Your Object Controller's binding's should looks something more like:

objectController.entityName = @"Account";
objectController.automaticallyPreparesContrect = TRUE;
[objectController bind:@"managedObjectContext" 
              toObject:[NSApp delegate]
           withKeyPath:@"managedObjectContext"
               options:@{NSRaisesForNotApplicableKeysBindingOption : @(YES)
                        }];

button.target = objectController;
button.action = @selector(add:);

[textField bind:@"stringValue"
       toObject:objectController
    withKeyPath:@"selection.name"
        options:@{NSConditionallySetsEditableBindingOption : @(YES),
                  NSNoSelectionPlaceholderBindingOption : @"No Selection"
                /* etc... */          
                 }];

You DON'T bind the managedObjectContext to the controller's path or value, that's not how it works. Formally, you don't have to bind this objectControllers value to anything in order to create a new Account and have it's props show up in the text field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM