核心数据关系问题

时间:2015-11-13 13:17:39

标签: ios core-data relationship

我正在设计一个核心数据库。当我使用单个实体时我很好。但我对关系非常困惑。我还附上了一张图片,以使事情更加清晰

我之前有一个实体为“员工”

我创建了“地址”作为一个新实体,我已经建立了它们之间的关系。但我无法理解如何编写完美的程序。我经历了很多教程,但没有用。我已经提到了我到目前为止所做的所有编码。请消化并逐步指导我接下来应该做什么来跨越这段关系?

我正在使用EmployeeTableViewController来显示员工的名称,并使用CustomViewController输入名称。现在我有了一个有关系的“地址”实体。如何从这里开始?

“员工”与“员工”之间的关系“地址” - 命名为“属于” “地址”与“地址”之间的关系“员工” - 命名为“常驻”

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

在此之后我应该怎样做才能实现我的目标,即如何通过CoreData Relationship上下文为每位员工添加地址。

提前致谢。

请帮忙。

Image of Data Model

Image Of Main Story Board

1 个答案:

答案 0 :(得分:1)

这是简单的版本。首先,为了清楚起见,我将其更改为使用托管对象子类。

假设您有一个名为adrplacetextfield的内容用于输入地址,请创建一个Address对象以匹配您的Employee。设置place的方式与Employee获取数据的方式相同。设置关系addBelongsfromObject并保存。 (核心数据负责反向关系。)

在真实的应用中,您需要确保有有效的条目,并且您没有创建重复项,但编程基础与不同的对象有很大不同使用核心数据 - 反向关系除外。

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