Core Data relationship question
Core Data relationship question
- Subject: Core Data relationship question
- From: Alex Reynolds <email@hidden>
- Date: Mon, 13 Aug 2007 02:16:46 -0400
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