Core Data uniqueness
Asked Answered
D

1

1

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.

Dipper answered 10/3, 2010 at 8:56 Comment(0)
W
0

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;
    }
}
Workaday answered 10/3, 2010 at 9:5 Comment(7)
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?Dipper
You probably want to do it before you dimiss the view controller where the user edits the values.Workaday
I am editing in the table view so the view controller hangs around throughout the app.Dipper
I see. Then you probably want to validate when the cell or field loses focus (maybe when the keyboard is dismissed).Workaday
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?Dipper
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...Dipper
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.Workaday

© 2022 - 2024 — McMap. All rights reserved.