Re: Core Data relationship question
Re: Core Data relationship question
- Subject: Re: Core Data relationship question
- From: Felix Franz <email@hidden>
- Date: Mon, 13 Aug 2007 10:39:04 +0200
Hi Alex,
you can simply use:
- (BOOL) initializeDisplayEntityForServer: (NSManagedObject *)
targetObject
{
NSManagedObject* display = [NSEntityDescription
insertNewObjectForEntityForName: @"Display" inManagedObjectContext:
managedObjectContext];
// fill out Display object ..
...
// set the relationship:
[[targetObject mutableSetValueForKey: @"displays"] addObject: display];
return YES;
}
The objectID will be permanent after invoking save: on the
ManagedObjectContext.
(As described in <file:///Developer/ADC Reference Library/
documentation/Cocoa/Reference/CoreDataFramework/Classes/
NSManagedObject_Class/Reference/Reference.html#//apple_ref/occ/instm/
NSManagedObject/objectID>).
Cheers,
felix
(assuming targetObject
On Aug 13, 2007, at 8:16 AM, Alex Reynolds wrote:
I have a question about how to set the value of a relationship in
an entity instance.
I have two entities, with the following schema:
Server
-> name (attribute)
-> ipAddress (attribute)
-> displays (relationship)
Display
-> bitDepth (attribute)
-> name (attribute)
-> resolution (attribute)
-> server (relationship)
I have a one-to-many relationship between Server and Display — that
is, a Server instance can be associated with multiple Display
instances. A Display instance is associated with one Server instance.
I know how to assign values to the attribute keys, but I am
wondering how to go about assigning a value to a Display instance,
such that the "server" relationship is associated with a specific
Server instance.
I have two methods below: - (BOOL) initializeServerEntity and -
(BOOL) initializeDisplayEntityForServer:targetObject
The first method passes the specific, matching server entity to the
second (initializeDisplayEntityForServer) method.
What calls do I make to associate display information (bitDepth,
name, resolution) to the specific server I am passing in?
Right now I print out the entity ID, but this changes every time I
run the program, so I don't know if I can use this.
Thanks,
Alex
- (BOOL) initializeServerEntity
{
//
// generate list of server entities
//
NSFetchRequest *oldServersFetchRequest = [[[NSFetchRequest alloc]
init] autorelease];
[oldServersFetchRequest setEntity: [[managedObjectModel
entitiesByName] valueForKey: @"Server"]];
NSArray *oldServers = [managedObjectContext executeFetchRequest:
oldServersFetchRequest error: nil];
NSArray *oldServerNames = [oldServers valueForKey: @"name"];
// NSLog(@"- initializeServerEntity : count of oldServerNames : %d
\n%@", [oldServerNames count], oldServerNames);
//
// determine if "localhost" server is in list of names from Server
entities
//
unsigned index=0;
BOOL localhostIsInServerEntity=NO;
while (index < [oldServerNames count])
{
if ([[oldServerNames objectAtIndex:index]
isEqualToString:@"localhost"])
{
localhostIsInServerEntity = YES;
[self initializeDisplayEntityForServer:[oldServers
objectAtIndex:index]];
//
// save changes to managed object context
//
[managedObjectContext save: nil];
}
index++;
}
//
// if "localhost" is not in Server entities, add to Server managed
object context
//
if (localhostIsInServerEntity == NO)
{
NSManagedObject *server = [NSEntityDescription
insertNewObjectForEntityForName: @"Server"
inManagedObjectContext:managedObjectContext];
[server setValue:@"localhost" forKey: @"name"];
[server setValue:@"127.0.0.1" forKey: @"ipAddress"];
//
// save changes to managed object context
//
[managedObjectContext save: nil];
}
}
- (BOOL) initializeDisplayEntityForServer: (NSManagedObject *)
targetObject
{
//
// this objectID value changes every time the application runs
//
NSLog(@"- initializeDisplayEntityForServer : targetObjectID : %@",
[targetObject objectID]);
return YES;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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