The interaction of data and sampling in many ways

I am new to master data, so I have a few questions. I will ask two

1) I have two objects named Team and TeamMembers. They have many relationships, i.e. One team can have many members. First of all, look at the diagram and .h files with such models and tell me if I managed to establish the right relationship between Team and TeamMembers (I have the feeling that I made the opposite relationship).

enter image description here

Teams.h

#import <CoreData/CoreData.h> @class TeamMembers; @interface Teams : NSManagedObject { } @property (nonatomic, retain) NSString * team_name; @property (nonatomic, retain) NSString * color; @property (nonatomic, retain) NSString * points; @property (nonatomic, retain) TeamMembers * members; @end 

TeamMembers.h

 #import <CoreData/CoreData.h> @class Teams; @interface TeamMembers : NSManagedObject { } @property (nonatomic, retain) NSString * member_name; @property (nonatomic, retain) NSSet* teams; @end @interface TeamMembers (CoreDataGeneratedAccessors) - (void)addTeamsObject:(Teams *)value; - (void)removeTeamsObject:(Teams *)value; - (void)addTeams:(NSSet *)value; - (void)removeTeams:(NSSet *)value; @end 

2) Please, I need a sample code to insert a team and then insert its team members. Also how to get team members of a particular team.


EDITED I use the following code snippet to insert teams and team members into groups, but it does not return all team members to NSSet. It returns only one member of the team in the result set.

  self.context = [del managedObjectContext]; Teams *teamobj = [NSEntityDescription insertNewObjectForEntityForName:@"Teams" inManagedObjectContext:context]; teamobj.team_name = teamname.text; teamobj.color = [NSString stringWithFormat:@"%d", color]; teamobj.points = [NSString stringWithFormat:@"%d", 0]; for(UITextField *view in self.scrollview.subviews) { if([view isKindOfClass:[UITextField class]]) { if ([view tag] == 99) { if (![view.text isEqualToString:@""]) { noone = YES; TeamMembers *teammember = [NSEntityDescription insertNewObjectForEntityForName:@"TeamMembers" inManagedObjectContext:context]; teammember.member_name = view.text; teammember.teams = teamobj; [teamobj addMembersObject:teammember]; } } } } if (![context save:&error]) { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Unable to save team at the moment." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; } 
+7
source share
3 answers

Yes, you did the opposite relationship. You have a team member that has many teams and a team that belongs to one member. You can indicate the arrows on the relationship line: the double arrow means “has many”, and one arrow means “has one” or “belongs” (read in the direction indicated by the arrow).

You must switch the direction in the model navigator, destroy the existing generated classes, and then regenerate them.

When you set up the relationship correctly, you can use the generated accessors to add members to the team and retrieve members for a specific team.

EDIT to show an example of adding a new team member to a team:

 TeamMember *newTeamMember = [NSEntityDescription insertNewObjectForEntityForName:@"TeamMember" inManagedObjectContext:context]; newTeamMember.team = <existing team managed object> 

It is so simple. Core Data will ensure that feedback is updated automatically, so the teamMembers team will include the new member you just added.

+8
source

I hope the link works. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdCreateMOs.html#//apple_ref/doc/uid/TP40001654-CJBDBHCB

Apple documents are really clear.

NSManagedObject *newTeam = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:context];

+1
source

My answer may be too late, but be sure to check the "To-Many Relationship" in the Relationships property sheet. Exclude the rule, as well as "Interact with many."

Perhaps this will also be useful, I am inserting related objects from NSMutableArray using the following code:

 NSSet *itemsSet; [itemsSet setByAddingObjectsFromArray:YOUR_NSMUTABLEARRAY]; [MAIN_OBJECT addItems:itemsSet]; 
+1
source

All Articles