0

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 M-V-C.

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
)
6
  • In applicationDidFinishLaunching, try an NSLog on the managedObjectContext property... just make sure the lazy getters create the stack and the MOC's not nil for some reason.
    – stevesliva
    Commented May 29, 2014 at 2:37
  • NSLog: AppDelegate:<NSManagedObjectContext: 0x6080001c2df0> Context model. The binding between the ObjectController and the Text Field appears to be correct because the drop down list of options available to select for the Text Field's Model Key Path does present the name field for me to choose (I didn't have to type it in).
    – torpedo51
    Commented May 29, 2014 at 4:43
  • Huh-- Chuck is right below that it sounds like a MOM issue. Do you have a backlevel model version loading or some such thing?
    – stevesliva
    Commented May 29, 2014 at 15:14
  • I don't have any coding. Its all generated by XCode when I select a Core Data Application project. I didn't touch any of that. All I'm doing is configuring the bindngs in the IB. Maybe I'm missing a prerequisite Outlet to be connected, but the drop down list is allowing me to select my entity's field ("name") accurately.
    – torpedo51
    Commented May 29, 2014 at 19:06
  • Yeah, but I meant the non-code xcdatamodeld file that you edit in XCode might be screwy somehow. Like set to a backlevel version. Or maybe you have "Account" marked as an abstract entity.
    – stevesliva
    Commented May 29, 2014 at 20:57

2 Answers 2

2

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.

1
  • I'm bumping this answer up because it's the most correct. Somewhere between creating the model and running the program, I added an attribute to an entity that versioned the model. I simply increased the version on my my model and everything works as advertised. Thanks for the persistence helping me sift through it.
    – torpedo51
    Commented May 30, 2014 at 19:45
1

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.

5
  • The way the IB bindings are described actually sounds correct-- the OP states the MOC is bound in the parameters section, which is where you find the place to bind the MOC to the ObjectController in IB.
    – stevesliva
    Commented May 29, 2014 at 2:31
  • Right, but managedObjectContext shouldn't be bound to a controller's "model key path"...
    – iluvcapra
    Commented May 29, 2014 at 16:37
  • "bind to App Delegate, controller key = null, model key path = managedObjectContext" -- this is a perfectly correct way to set up the context binding in Interface Builder. What it describes is not different than the first binding in your code. I still don't see how your answer "objectController's content shouldn't be bound to a managedObjectContext" is applicable -- nothing suggests that's been done.
    – stevesliva
    Commented May 29, 2014 at 21:45
  • Oh I get it. IB is a pain.
    – iluvcapra
    Commented May 30, 2014 at 17:11
  • It refers to the standard way of binding to a MOC in the bindings inspector for an object in Interface Builder.... yeah. FWIW, I don't understand the "controller key" vs. "model key path" semantics there, versus the programmatic bindings.
    – stevesliva
    Commented May 30, 2014 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.