Using an empty string as the default value for a Core Data attribute in the XCode modeller

  • Hi,

    I'm fairly new with Core Data and I've run into a problem with string
    default values.. any help would be appreciated.

    In the modeling view, you can supply default values for each entity
    attribute. I want a string attribute with default value @"" (not null/
    nil):

    I simply unticked the "optional" checkbox and left the default value
    field blank, thinking that this ought to do the trick.. unfortunately
    I promptly get a validation error when I try to quit the program
    without supplying a value; I leave all other fields untouched so it's
    not a reg. ex., min or max length problem. Supplying a different
    default value makes the validation error go away.

    My guess is that leaving the default value field blank means "no
    default value" rather than "use the empty string as the default
    value", but that will mean that I either have to programmatically set
    the default value to @"" or need to test every time I read this
    attribute whether it is null or contains a string. Both of those seem
    to go against the grain of putting default values and validation into
    the model rather than the code..

    Is there any trick to use the modeler to supply @"" as a default
    value? I find it hard to type an empty string ;-)

    Thanks for your help.

    Best regards,

    Frank
  • Hello Frank,

    Why don't you just subclass NSManagedObject for this entity and set
    this value explicitly to an empty string when a new instance is
    created using -awakeFromInsert?

    Cheers,
    Annard

    On 5 Sep 2007, at 12:33, Frank Reiff wrote:

    > Is there any trick to use the modeler to supply @"" as a default
    > value? I find it hard to type an empty string ;-)
  • Hi Annard,

    > Why don't you just subclass NSManagedObject for this entity and set
    > this value explicitly to an empty string when a new instance is
    > created using -awakeFromInsert?

    Yes, I could do that but I was hoping to avoid creating subclasses
    for each and every class that takes an empty string as a default
    value.. I'm using Core Data as a database rather than as an
    persistent object model, so if there is any way of avoiding doing
    this programatically it would really help me out..

    Up to now I was using custom classes and a simple XML storage format
    in conjunction with Cocoa bindings to get a quick and effective way
    of "remembering" UI settings that are too complicated for
    NSUserDefaults. This is kind of cool because I can take the settings
    and store them into droplets, share them over the internet, etc..

    For my latest project I'm using Core Data for overcoming Cocoa's
    memory management limitations with very large data sets (instead of
    mucking around with NSAutorelease pools and profiler tools). With
    this solution even the machine's RAM is no longer a hard limit..

    I was quite happy with those developments and given how well Core
    Data and Cocoa Bindings seemed to be integrated I thought this might
    be an opportunity to get rid of my custom bindings code and make use
    of the built-in modelers and validation support.. unfortunately once
    I start having to subclass everything, I'm  stuck with the same
    overhead as before and with more constraints + Apple bugs rather than
    my own custom bugs which I prefer ;-)

    I'm not keen on having to import the class definitions into each nib
    that uses them, writing boiler-plate initialization code, source
    manage them, etc..

    Still I'm only starting to learn about Core Data and while it looked
    like a solution to everything, learning more about its limits early
    on might not be a bad thing.. there's still time to ditch it for what
    it's no good at and keep it for where it makes sense.

    I take it that there simply isn't any way then to put an empty string
    as a default value in the modeler? Sigh..

    Thanks for your help.

    Best regards,

    Frank
  • On Sep 5, 2007, at 3:33 AM, Frank Reiff wrote:

    > I simply unticked the "optional" checkbox and left the default value
    > field blank, thinking that this ought to do the trick..
    >
    Umm, no, that simply means that it becomes a required value.

    > My guess is that leaving the default value field blank means "no
    > default value" rather than "use the empty string as the default
    > value", but that will mean that I either have to programmatically
    > set the default value to @"" or need to test every time I read this
    > attribute whether it is null or contains a string. Both of those
    > seem to go against the grain of putting default values and
    > validation into the model rather than the code..
    > Is there any trick to use the modeler to supply @"" as a default
    > value?
    >
    Modify the model immediately after reading it from disk.

    mmalc
  • Dear mmalc,

    > Modify the model immediately after reading it from disk.

    Right, so if I modify the model programmatically when it first gets
    loaded, I can have a @"" default value without needing to sub-class
    NSManagedObject.. excellent.

    I hadn't even suspected that you could programatically modify the
    model, but it should work fine:

    > Editing Models Programatically
    >
    > Managed object models are editable until they are used by an object
    > graph manager (a managed object context or a persistent store
    > coordinator). This allows you to create or modify them dynamically.
    > However, once a model is being used, it must not be changed. This
    > is enforced at runtime—when the object manager first fetches data
    > using a model, the whole of that model becomes uneditable. Any
    > attempt to mutate a model or any of its sub-objects after that
    > point causes an exception to be thrown. If you need to modify a
    > model that is in use, create a copy, modify the copy, and then
    > discard the objects with the old model.
    Thanks for the tip.

    Best regards,

    Frank

    On 5 Sep 2007, at 17:34, mmalc crawford wrote:
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today