views:

55

answers:

1

Hi everyone,

Is there any way I can validate a value updated in a Core Data entity's property against values of the property in other entities in the collection?

At the moment I create an entity with some default values, add it to arrangedObjects, then get the user to modify the various property values. However, I would like to check a particular property and make sure there're no other entities in the array with the same value for that property. What's the best way to do this?

Many thanks, Dany.

A: 

Manually checking is only a few lines of code with a fast enumeration loop:

BOOL unique = YES;
for (NSManagedObject *obj in collection) {
    if (obj.property == value) {
        unique = NO;
        break;
    }
}
gerry3
Thanks but where can I do this from? Sorry, I still have my head stuck in the .NET event model - is there something similar to "updating" event as such?
DanyW
You probably want to do it before you dimiss the view controller where the user edits the values.
gerry3
I am editing in the table view so the view controller hangs around throughout the app.
DanyW
I see. Then you probably want to validate when the cell or field loses focus (maybe when the keyboard is dismissed).
gerry3
Thanks - that makes sense. One more question if I may please. I have had a look through the reference for NSTextFieldCell and cannot find a way of handling the field/cell losing focus. In fact, the whole event thing in Cocoa is still very fuzzy for me. Can you please point me in the right general direction for event handling?
DanyW
Hang on...just noticed something...you keep referring to keyboard being dismissed. Are you referring to iPhone apps? I am writing a Mac app at the moment...
DanyW
Oops, I am used to thinking in terms of iPhone. I still think you should look at when the cell or field loses focus, but I haven't worked with an NSTableView.
gerry3