CoreData validation strangeness
CoreData validation strangeness
- Subject: CoreData validation strangeness
- From: Shaun Wexler <email@hidden>
- Date: Sat, 11 Mar 2006 12:29:24 -0800
I'm seeing some strange behavior with a CoreData validation method.
A bound text field validates immediately and updates continuously. I
am fetching with a predicate to prevent duplicate entries, and want
to check each character. If a match is found, NO is returned (it
also returns a nil NSError). If the subsequent keypress is
Backspace, no validation is performed?!! It appears that the
NSArrayController or something else in KVO is caching the previous
validation string incorrectly, because if I type "A", backspace, then
"A" again, no validation is performed the 2nd time. Is this the
correct behavior? I believe my logic is correct. The desired result
is to bind the "OK" button enable to newAccountNameIsUnique, so that
it is disabled for nil, zero-length, or like-matching an existing
string:
- (BOOL)validateNewAccountName:(id *)accountName error:(NSError **)
outError
{
BOOL isValid;
if ((isValid = accountName && *accountName))
{
static NSFetchRequest *fetchRequest = nil;
if (!fetchRequest) {
fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription
entityForName:@"Account" inManagedObjectContext:[self
managedObjectContext]]];
}
[fetchRequest setPredicate:[NSPredicate
predicateWithFormat:@"accountName like[cd] %@", *accountName]];
NSArray *array = [managedObjectContext
executeFetchRequest:fetchRequest error:outError];
isValid = !((outError && *outError) || !array) && [array
count] == 0;
}
if (newAccountNameIsUnique != isValid) {
[self willChangeValueForKey:@"newAccountNameIsUnique"];
newAccountNameIsUnique = isValid;
[self didChangeValueForKey:@"newAccountNameIsUnique"];
}
return isValid;
}
- (NSString *)newAccountName
{
return newAccountName;
}
- (void)setNewAccountName:(NSString *)name
{
if (name != newAccountName) {
[newAccountName autorelease];
newAccountName = [name retain];
}
}
- (int)newAccountType
{
return newAccountType;
}
- (void)setNewAccountType:(int)type
{
newAccountType = type;
}
- (BOOL)newAccountNameIsUnique
{
return newAccountNameIsUnique;
}
Hmmmmmm. Any insight would be appreciated... TIA. :0)
--
Shaun Wexler
MacFOH
http://www.macfoh.com
"A person who never made a mistake never tried anything new." -
Albert Einstein
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden