• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data : calling validation in awakeFromInsert problem ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Core Data : calling validation in awakeFromInsert problem ?


  • Subject: Core Data : calling validation in awakeFromInsert problem ?
  • From: Eric Morand <email@hidden>
  • Date: Mon, 5 Sep 2005 18:42:22 +0200

Hi list !


I'm trying to implement validation on my managed objects and am facing a strange problem.


I have a managed object with one property named "name". This property is a string. I want this property to be unique in the managed context of the object, so I've implemented "validateName..." method in my managed object subclass (see code below).

Next, I want new objects inserted into the context to have a name set to a validated one (that means : unique). So I have implemented "awakeFromInsert..." method in my subclass that set the name property to a valid one.

Here is the code for my custom class (implementation only, there is nothing of interest in the header) :

//
//  TestObject.m
//  CoreData
//
//  Created by Eric on 05/09/05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.
//

#import "TestObject.h"


@implementation TestObject

- (NSString *)name
{
    NSString * tmpValue;

    [self willAccessValueForKey: @"name"];
    tmpValue = [self primitiveValueForKey: @"name"];
    [self didAccessValueForKey: @"name"];

    return tmpValue;
}

- (void)setName:(NSString *)value
{
    [self willChangeValueForKey: @"name"];
    [self setPrimitiveValue: value forKey: @"name"];
    [self didChangeValueForKey: @"name"];
}

- (void)awakeFromInsert
{
// Here I want the default name to be valid !
// In case objects already exit with the name "TestObjectName",
// append "(index++)" at the end of the name until this name doesn't already exists
// in the managed object context


NSString * validName = nil;
NSString * defaultName = [NSString stringWithString:@"TestObjectName"];


    validName = defaultName;

    int index = 0;

    while ( ![self validateValue:&validName forKey:@"name" error:nil] )
    {
        index++;

validName = [defaultName stringByAppendingFormat:@" (%d)", index];
}


    [self setName:validName];
}

- (BOOL)validateName: (id *)valueRef error:(NSError **)outError
{
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name == %@", *valueRef];


NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"TestObject" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest * fetchRequest = [[[NSFetchRequest alloc] init] autorelease];


    [fetchRequest setEntity:entityDescription];
    [fetchRequest setPredicate:predicate];

NSArray * results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil];

return ( [results count] < 1 ) || ( [results objectAtIndex:0] == self );
}


@end






Now, everything looks fine to me.

I'm using a simple interface to test the insertion process : in fact it is the standard interface created automatically when alt-dragging the object from Xcode to IB - one table view, 3 buttons (Fetch, Add, Remove), a text field to enter name and a search field. Then, the problems begin : when I click on the add button, TWO objects are added to the table view !


If I replace the line :

while ( ![self validateValue:&validName forKey:@"name" error:nil] )


by :

while ( index < 5 )

it works like a charm (every object is created with the same name "TestObjectName (5)", of course). So it seems that calling the validation method in the awakeFromInsert is causing a problem but I can't put my finger on it.


Do someone know somethign about it ?
And do you think I'm using the right process to validate my name property and mak sure my object are inserted with valid name ?





Thanks,



Eric.
_______________________________________________
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


  • Follow-Ups:
    • Re: Core Data : calling validation in awakeFromInsert problem ?
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Help with data browser
  • Next by Date: [cocoa scripting] default sub container
  • Previous by thread: don't understand pdfview and pdfkit
  • Next by thread: Re: Core Data : calling validation in awakeFromInsert problem ?
  • Index(es):
    • Date
    • Thread