Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data insert and fetch to-many relationship entities

I am new to core data therefore I have got few questions. I'll ask two

1) I have two entities named Team and TeamMembers. They have to-many relationship i.e. one team can have many members. First please take a look at following diagram and .h files of there models and let me know whether I have made correct to-many relationship between Team and TeamMembers (I have a feeling that I have made 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 sample code for inserting Team then inserting its team members. Also How to fetch team members of particular team.


EDITED I am using following piece of code to insert into Teams and Team members entities but it is not returning all team members in NSSet. It is returning only one team member 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];            
            }
like image 267
Ayaz Alavi Avatar asked Oct 08 '11 14:10

Ayaz Alavi


People also ask

What is fetched property in Core Data?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.

What is transformable in Core Data?

When you declare a property as Transformable Core Data converts your custom data type into binary Data when it is saved to the persistent store and converts it back to your custom data type when fetched from the store. It does this through a value transformer.

What is inverse relationship in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.


1 Answers

Yes, you have made the opposite relationship. You have a team member having many teams and a team belonging to one member. You can tell by the arrows on the relationship line: the double arrow means 'has many' and the single arrow means 'has one' or 'belongs to' (read in the direction that the arrow is pointing).

You should switch the direction in the model navigator, trash the existing generated classes then re-generate them.

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

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

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

It's as simple as that. Core Data will make sure that the reverse relationship is updated automatically, so the teamMembers collection of the team will include the new member you just added.

like image 113
Robin Summerhill Avatar answered Sep 28 '22 01:09

Robin Summerhill