• 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
awakeFromInsert question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

awakeFromInsert question


  • Subject: awakeFromInsert question
  • From: Guillaume Rager <email@hidden>
  • Date: Fri, 1 Jul 2005 15:09:36 +0200

Hi list,

i would like to add some default values using awakeFromInsert method into my NSManagedObject subclass named Task.

to add some strings, dates or int values its very easy, i just have to do that:

- (void) awakeFromInsert
{
    [super awakeFromInsert];

    [self setValue:@"test" forKey:@"title"];
}

to set a default title for example.

But i would like to do add a NSManagedObject subclass category. This entity is linked to Task using a relationship. I have a list of existing categories (work, home,...) and i want to setup my task with a "work" category. So this is what i've done into awakeFromInsert

- (void) awakeFromInsert
{
    [super awakeFromInsert];

// create the fetch request for the recipes
NSFetchRequest* fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Category" inManagedObjectContext:[self managedObjectContext]]];


    // set the affected stores
    id store = [[self objectID] persistentStore];
    if (store != nil) {
    [fetchRequest setAffectedStores:[NSArray arrayWithObject:store]];
    }

// set the predicate
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"value = %@",@"work"]];


NSError *error = nil;
NSArray *array = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];


    if([array count]>0)
        [self setValue:[array lastObject] forKey:@"category"];
}

So my question is simple, is there any better solution to link a new entity (Task) to an existing entity (Category) ?

Thank you in advance.



Guillaume

-----------
GUMITECH - France
http://www.gumitech.com

"Chaud cocoa, chaud chocolat !"

_______________________________________________
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: awakeFromInsert question
      • From: SA Dev <email@hidden>
  • Prev by Date: Re: Floating panel visibility
  • Next by Date: Newbie Questions re NSDecimal
  • Previous by thread: Re: Flat Button Controls in Tiger
  • Next by thread: Re: awakeFromInsert question
  • Index(es):
    • Date
    • Thread