Re: awakeFromInsert question
Re: awakeFromInsert question
- Subject: Re: awakeFromInsert question
- From: SA Dev <email@hidden>
- Date: Fri, 1 Jul 2005 11:47:31 -0400
Guillaume:
For to-many relationships, you first have to get the
mutableSetValueforKey ...
If 'Category' has a to-many relationship to 'Task' called "tasks",
you get a mutable set of tasks from that category instance:
NSMutableSet * categoryTasks = [aCategory
mutableSetValueForKey:@"tasks"];
You then add the new task to that mutable set:
[categoryTasks addObject:aNewTask];
You can remove a task from that mutable set as well:
[categoryTasks removeObject:anotherTask];
I hope this helps.
On Jul 1, 2005, at 9:09 AM, Guillaume Rager wrote:
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:
40silentalcove.net
This email sent to email@hidden
_______________________________________________
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