Reputation: 547
I've created a Core Data model and have created a category class that contains methods for updating and deleting the data. I'm trying to add validation methods to the class and attempting to use KVC but struggling a little.
My problem is this, my validate methods are only triggered when I actually save the context [context save:&internalError], they are working correctly but the save process also completes. My question is this, when is the validation triggered can it be triggered before the save, or am I doing this completely wrong?
My code:
+(int)doSmeThing:(InstructionMessageObject *)message inManagedObjectContext:(NSManagedObjectContext *)context error:(NSError **)error {
NSError *internalError = nil;
int timeStamp = [[NSDate date] timeIntervalSince1970];
NSManagedObject *newMessageObject = [NSEntityDescription insertNewObjectForEntityForName:@"CoreDataTable"inManagedObjectContext:context];
[newMessageObject setValue:message.productCode forKey:@"productCode"];
[newMessageObject setValue:message.quantity forKey:@"quantity"];
///////////////////////////////////////
// Need to validate HERE before save //
///////////////////////////////////////
if (![context save:&internalError]) {
*error = internalError;
return NO;
}
return YES;
}
- (BOOL)validateProductCode:(id *)ioValue error:(NSError **)outError {
*outError = nil;
if ([*ioValue integerValue] < 1 ) {
*outError = [NSError errorWithDomain:@"domain" code:101 userInfo:[NSDictionary dictionaryWithObject:@"Invalid Product Code" forKey:NSLocalizedDescriptionKey]];
return NO;
}
return YES;
}
- (BOOL)validateQuantity:(id *)ioValue error:(NSError **)outError {
*outError = nil;
if ([*ioValue integerValue] < 1 ) {
*outError = [NSError errorWithDomain:@"domain" code:102 userInfo:[NSDictionary dictionaryWithObject:@"Invalid Quantity" forKey:NSLocalizedDescriptionKey]];
return NO;
}
return YES;
}
Upvotes: 0
Views: 1009
Reputation: 125007
From the docs:
It is important to understand that how to validate is a model decision, when to validate is a user interface or controller-level decision (for example, a value binding for a text field might have its “validates immediately” option enabled).
and also:
There is nothing to disallow an in-memory object from becoming inconsistent on a temporary basis. The validation constraints are applied by Core Data only during a “save” operation or upon request (you can invoke the validation methods directly as and when you wish). Sometimes it may be useful to validate changes as soon as they are made and to report errors immediately.
Note that when they say "you can invoke the validation methods directly" I don't think they mean that you should actually call the property-specific validation methods, as there's a note just after that:
Important: If you do implement custom validation methods, you should typically not invoke them directly. Instead you should call validateValue:forKey:error: with the appropriate key. This ensures that any constraints defined in the managed object model are also applied.
So, the fact that your validation methods are only called when the context is saved isn't surprising -- that's when Core Data does its validation. That said, you're free to validate more frequently, and if you choose to do that you should do it by calling -validateValue:forKey:error:
.
Upvotes: 2