small medium large xlarge

Back to: All Forums  Core Data
Me_pragsmall
30 Jan 2009, 17:38
C.A. (6 posts)

I tried to find a solution to this but have been unsuccessful thus far so it would be nice if it were mentioned in the book. My apologies if it’s in the errata already but I didn’t see it there.

You can define a Default Value for an attribute but how to go about overriding the default value on a relationship? For example in GrokkingRecipes there’s an Authors entity which populates the Authors pop-up. Recipe has a relationship with Authors. If the recipe is new the default value for Author is “No Value”.

While I understand that it needs to say something to show nil it would be good to be able to change that to something other than “No Value” which I find misleading.

Thanks!

Avatarsmall_pragsmall
07 Feb 2009, 03:25
Marcus S. Zarra (284 posts)

That can be changed in the configuration of the binding. However if you want to set up a default relationship (which would be a better user experience imho) then you would want to create a subclass of NSManagedObject for Recipe and override its -awakeFromInsert method. In that method you can then set the relationship.

Me_pragsmall
07 Feb 2009, 20:27
C.A. (6 posts)

Excellent! I bound it to the Selected Value and put a default string into the Null Placeholder field.

Thanks!

Me_pragsmall
08 Feb 2009, 16:31
C.A. (6 posts)

Oops! That would work for an attribute but not a relationship. I need to use Selected Object which doesn’t have any Placeholders. So where would I override “No Value” in the bindings? I guess I could put something into awakeFromInsert but my intention was to have the pop-up name/title be “Select blah…” instead of having a separate label off to the side.

Avatarsmall_pragsmall
10 Feb 2009, 02:22
Marcus S. Zarra (284 posts)

That gets a little more complicated. In that case you want the first item in the list to be “Select blah…” and have the list be an array of strings with the remaining strings being the display text that you can associate back to your objects. Then on the back side, when the user makes a selection you can (in the @-setXXXX:@ method) pair up to the actual object and if they select “Select Blah…” then you set the relationship to nil.

Its ugly but that is the way it works. I would suggest, however, to instead just create a default relationship if it makes sense to guess.

You must be logged in to comment