user5479794
user5479794

Reputation:

Core data Relationship problems

I am designing a core data database. I am fine when I am using a single entity. But I am very much confused about relationships. I have also attached a image to make things more clear

I earlier had one entity as "Employee"

I created "address" as a new entity and I have established relationship between them. But I can't understand how to code to have a perfect program going. I have gone through many tutorials but of no use. I have mentioned all the coding that I have made till now. Please sugest and give me step by step instruction of what I should be doing next to cross this hurdle of relationship?

I am using EmployeeTableViewController to display the names of the employees and CustomViewController to enter the name. Now I have a "address" entity with a relationship. How to proceed from here on?

Relationship between "Employee" & "Address" - named as "belongsfrom" Relationship between "address" & "Employee"-named as "resident"

 CustomViewController.m

- (IBAction)save:(id)sender
{
    NSManagedObject *employee=[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_managedObjectContext];
    [employee setValue:self.empnametextfield.text forKey:@"empname"];
    [employee setValue:self.empidtextfield.text forKey:@"empid"];

    NSError *error;
    if (![self.managedObjectContext save:&error])
    {
        NSLog(@"Problem in saving -%@",[error localizedDescription]);
    }
}

EmployeeTableViewController.m

@interface EmployeeTableViewController ()

@property(strong,nonatomic)NSMutableArray *employee;
@property(strong,nonatomic)NSFetchRequest *fetchrequest;
@property(strong,nonatomic)Employee *EMPLOYEE;
@property(strong,nonatomic)NSArray *fetchedobjects;

@end

@implementation EmployeeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchdata];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }
-(void)viewDidAppear:(BOOL)animated
{
    [self fetchdata];
    [self.tableView reloadData];
}
-(void)fetchdata
{
    _fetchrequest=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"Employee" inManagedObjectContext:_managedObjectContext];
    [_fetchrequest setEntity:entity];

    NSError *error;
    _fetchedobjects=[_managedObjectContext executeFetchRequest:_fetchrequest error:&error];
    if (_fetchedobjects!=nil)
    {
        _employee=[[NSMutableArray alloc]initWithArray:_fetchedobjects];
    }


}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _employee.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath];


    _EMPLOYEE=[_employee objectAtIndex:indexPath.row];
    cell.textLabel.text=_EMPLOYEE.empname;

    return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"seguereuse"])
    {
        CustomViewController *customViewController=[segue destinationViewController];
        customViewController.managedObjectContext=self.managedObjectContext;
    }
}

Now after this what should I do to realise my aim i.e How to add adresses via CoreData Relationship context for each employee.

Thanks in advance.

Please help.

Image of Data Model

Image Of Main Story Board

Upvotes: 0

Views: 65

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

Here's the simple version. First, I changed it to use managed object sub-classes for clarity.

Assuming you have something called adrplacetextfield for entering the address, create an Address object to match your Employee. Set its place the same way the Employee is given data. Set the relationship addBelongsfromObject and save. (Core Data takes care of the inverse relationship.)

In a real app, you'd want to make sure there were valid entries and that you weren't creating duplicates but the programming basics aren't much different from objects that don't use core data -- except for the inverse relationship.

- (IBAction)save:(id)sender
{
    Employee *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    employee.empname = self.empnametextfield.text;
    employee.empid = self.empidtextfield.text;

    Address *address = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:self.managedObjectContext];
    address.place = self.adrplacetextfield.text;
    [employee addBelongsfromObject:address];

    NSError *error;
    if (![self.managedObjectContext save:&error])
    {
        NSLog(@"Problem in saving -%@",[error localizedDescription]);
    }
}

Upvotes: 1

Related Questions