How do I bind ManagedObject's relationships to a view?
How do I bind ManagedObject's relationships to a view?
- Subject: How do I bind ManagedObject's relationships to a view?
- From: Daniel Wambold <email@hidden>
- Date: Tue, 1 Dec 2009 14:44:08 -0500
Hello, List. I am a novice with CoreData and bindings and I've stumped
myself with this problem. I have created an xcdatamodel that contains
3 objects: an Account object with a relationship property
authorizedUsers, which is a to-many relationship with a Person object.
The Person object has a reciprocal to-one relationship ("account")
with the Account object. I have a table view that shows all the
accounts, and which, when I click on a given account, should show, in
a separate table view, all the authorized users for that account. The
Person object has a read-only variable, nameAndID that concatenates
the first name, last name, and ID of the person entity. I feel like
I've tried every combination of bind to: and paths already. Right now,
I've only been successful by pushing a string into a scroll view with:
----(from the Account.m file)----
-(NSMutableString *)authorizedUsersNameID
{
NSMutableString *myString = [[[NSMutableString alloc] init]
autorelease];
NSSet *mySetOfAuthorizedUsers = [self valueForKey:authorizedUsersKey];
int i=0;
for (Person *aPerson in mySetOfAuthorizedUsers)
{
++i;
[myString appendFormat:@"%i: %@\n",i, [aPerson
valueForKey:nameAndIDKey]];
}
return myString;
}
----And in the view controller object:----
...
NSEntityDescription *personEntityDescription = [NSEntityDescription
entityForName:PersonEntity inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:personEntityDescription];
// Set predicate and sort orderings...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account
== %@)", mySelectedAccount];
[request setPredicate:predicate];
NSError *error;
NSArray *myAuthorizedUsers = [moc executeFetchRequest:request
error:&error];
if (myAuthorizedUsers == nil || [myAuthorizedUsers count] == 0)
{
// This is an error situation, as we should always have at least the
account owner as an authorized user.
NSLog (@"Search error: Authorized account users failed to match any
account.");
}
else
{
[myAccountUserTextView setString:@""]; // Clear out the text view.
for (NSManagedObject *anAuthorizedUser in myAuthorizedUsers)
{
[myAccountUserTextView insertText:[anAuthorizedUser
valueForKey:nameAndIDKey]];
[myAccountUserTextView insertText:@"\n"];
}
}
The problem is that, I also want to bind Transaction objects (same
relationships as Person) but they have more information such as dates,
amounts, and so forth. I'd much rather do this with bindings than to
create strings if it can be done. If you know what sort of settings I
need to establish in IB to set this up (such as which bindings and
what paths), please take a moment to set me straight. The CoreData
test model in IB doesn't do this for me, so I can't crib those
settings. I have working, subclassed controllers for each managed
entity type, and the data are clearly appearing in the managed object
(viz. the above code), so I suspect it's just my binding settings that
I've got wrong.
Thanks for your insight!
-Dan
_______________________________________________
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