开发者

Core data insert and fetch to-many relationship entities

开发者 https://www.devze.com 2023-04-11 20:47 出处:网络
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 pl

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).

Core data insert and fetch to-many relationship entities

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];            
            }


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.


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

The apple docs are really quite clear here.

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


Maybe my answer will be too late, but do not forget check "To-Many Relationship" in "Relationship" properties window. Delete Rule important as well as "To-Many Relationship".

Maybe this also will be helpful, I'm inserting related objects from NSMutableArray by the following code:

NSSet *itemsSet;
[itemsSet setByAddingObjectsFromArray:YOUR_NSMUTABLEARRAY];
[MAIN_OBJECT addItems:itemsSet];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号