0

我创建了一个核心数据模型并创建了一个类别类,其中包含用于更新和删除数据的方法。我正在尝试向该类添加验证方法并尝试使用 KVC 但有点挣扎。

我的问题是,我的验证方法仅在我实际保存上下文 [context save:&internalError] 时触发,它们工作正常,但保存过程也完成。我的问题是,什么时候触发验证可以在保存之前触发,还是我这样做完全错误?

我的代码:

+(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;
}
4

1 回答 1

2

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:.

于 2013-09-18T14:09:17.503 回答