NSArrayController display newly inserted objects twice Hi guys ! I'm facing a strange problem with an instance of NSArrayController. In my project, I have an entity with an attribute named "name". This attribute have to be unique, so I have a validation process that ensure the uniqueness : - (BOOL)validateName: (id *)valueRef error:(NSError **)outError { // "name" must be unique NSArray * results = nil; NSPredicate * predicate = nil; NSFetchRequest * fetchRequest = nil; predicate = [NSPredicate predicateWithFormat:@"(SELF != %@) AND (name LIKE %@)", self, *valueRef]; fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:[self entity]]; [fetchRequest setPredicate:predicate]; results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; if ( [results count] > 0 ) { return NO; } return YES; } I've also added the following methods to ensure that any newly inserted object has a unique name by defa! ult : ! - (void)awakeFromInsert { [self setUniqueName]; } - (void)setUniqueName { NSString * defaultName = NSLocalizedStringFromTableInBundle(@"NewCategory", @"Localizable", [NSBundle mainBundle], nil); NSString * uniqueName = defaultName; int index = 0; NSError * error = nil; while ( ![self validateValue:&uniqueName forKey:@"name" error:&error] ) { index++; uniqueName = [defaultName stringByAppendingFormat:@" (%d)", index]; break; } [self setName: uniqueName]; } This works perfectly in most cases...but the most important one : insertion using an NSArrayController. In this case, my managed object is inserted perfectly in the database, with a unique name, but the array controller inserts it twice in its content array ! It seems like the problem is due to this line in my validation method : results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; If I comment this line, everything works perfectly (but, obviously, the name is not unique). To ill! ustrat! e the problem, I've created a small project that can be do
NSArrayController display newly inserted objects twice Hi guys ! I'm facing a strange problem with an instance of NSArrayController. In my project, I have an entity with an attribute named "name". This attribute have to be unique, so I have a validation process that ensure the uniqueness : - (BOOL)validateName: (id *)valueRef error:(NSError **)outError { // "name" must be unique NSArray * results = nil; NSPredicate * predicate = nil; NSFetchRequest * fetchRequest = nil; predicate = [NSPredicate predicateWithFormat:@"(SELF != %@) AND (name LIKE %@)", self, *valueRef]; fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:[self entity]]; [fetchRequest setPredicate:predicate]; results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; if ( [results count] > 0 ) { return NO; } return YES; } I've also added the following methods to ensure that any newly inserted object has a unique name by defa! ult : ! - (void)awakeFromInsert { [self setUniqueName]; } - (void)setUniqueName { NSString * defaultName = NSLocalizedStringFromTableInBundle(@"NewCategory", @"Localizable", [NSBundle mainBundle], nil); NSString * uniqueName = defaultName; int index = 0; NSError * error = nil; while ( ![self validateValue:&uniqueName forKey:@"name" error:&error] ) { index++; uniqueName = [defaultName stringByAppendingFormat:@" (%d)", index]; break; } [self setName: uniqueName]; } This works perfectly in most cases...but the most important one : insertion using an NSArrayController. In this case, my managed object is inserted perfectly in the database, with a unique name, but the array controller inserts it twice in its content array ! It seems like the problem is due to this line in my validation method : results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; If I comment this line, everything works perfectly (but, obviously, the name is not unique). To ill! ustrat! e the problem, I've created a small project that can be do
- Subject: NSArrayController display newly inserted objects twice Hi guys ! I'm facing a strange problem with an instance of NSArrayController. In my project, I have an entity with an attribute named "name". This attribute have to be unique, so I have a validation process that ensure the uniqueness : - (BOOL)validateName: (id *)valueRef error:(NSError **)outError { // "name" must be unique NSArray * results = nil; NSPredicate * predicate = nil; NSFetchRequest * fetchRequest = nil; predicate = [NSPredicate predicateWithFormat:@"(SELF != %@) AND (name LIKE %@)", self, *valueRef]; fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:[self entity]]; [fetchRequest setPredicate:predicate]; results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; if ( [results count] > 0 ) { return NO; } return YES; } I've also added the following methods to ensure that any newly inserted object has a unique name by defa! ult : ! - (void)awakeFromInsert { [self setUniqueName]; } - (void)setUniqueName { NSString * defaultName = NSLocalizedStringFromTableInBundle(@"NewCategory", @"Localizable", [NSBundle mainBundle], nil); NSString * uniqueName = defaultName; int index = 0; NSError * error = nil; while ( ![self validateValue:&uniqueName forKey:@"name" error:&error] ) { index++; uniqueName = [defaultName stringByAppendingFormat:@" (%d)", index]; break; } [self setName: uniqueName]; } This works perfectly in most cases...but the most important one : insertion using an NSArrayController. In this case, my managed object is inserted perfectly in the database, with a unique name, but the array controller inserts it twice in its content array ! It seems like the problem is due to this line in my validation method : results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil]; If I comment this line, everything works perfectly (but, obviously, the name is not unique). To ill! ustrat! e the problem, I've created a small project that can be do
- From: Eric MORAND <email@hidden>
- Date: Thu, 7 Jun 2007 19:48:26 +0200
Hi guys !
I'm facing a strange problem with an instance of NSArrayController.
In my project, I have an entity with an attribute named "name". This
attribute have to be unique, so I have a validation process that
ensure the uniqueness :
- (BOOL)validateName: (id *)valueRef error:(NSError **)outError
{
// "name" must be unique
NSArray * results = nil;
NSPredicate * predicate = nil;
NSFetchRequest * fetchRequest = nil;
predicate = [NSPredicate predicateWithFormat:@"(SELF != %@) AND
(name LIKE %@)", self, *valueRef];
fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:[self entity]];
[fetchRequest setPredicate:predicate];
results = [[self managedObjectContext]
executeFetchRequest:fetchRequest error:nil];
if ( [results count] > 0 )
{
return NO;
}
return YES;
}
I've also added the following methods to ensure that any newly
inserted object has a unique name by default :
- (void)awakeFromInsert
{
[self setUniqueName];
}
- (void)setUniqueName
{
NSString * defaultName = NSLocalizedStringFromTableInBundle
(@"NewCategory", @"Localizable", [NSBundle mainBundle], nil);
NSString * uniqueName = defaultName;
int index = 0;
NSError * error = nil;
while ( ![self validateValue:&uniqueName forKey:@"name" error:&error] )
{
index++;
uniqueName = [defaultName stringByAppendingFormat:@" (%d)", index];
break;
}
[self setName: uniqueName];
}
This works perfectly in most cases...but the most important one :
insertion using an NSArrayController. In this case, my managed object
is inserted perfectly in the database, with a unique name, but the
array controller inserts it twice in its content array ! It seems
like the problem is due to this line in my validation method :
results = [[self managedObjectContext]
executeFetchRequest:fetchRequest error:nil];
If I comment this line, everything works perfectly (but, obviously,
the name is not unique).
To illustrate the problem, I've created a small project that can be
downloaded here (52kB) :
http://homepage.mac.com/eric.morand/.Public/Test.zip
Just open it in Xcode, compile and run it, and click on the "Add"
button.
Can someone help me on this ? Is there something I am doing wrong ? I
can't think of any other way to have a managed object automatically
inserted with a unique attribute.
Thanks by advance,
Eric.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden