【问题标题】:core data with multiple entities and uitableview具有多个实体和 uitableview 的核心数据
【发布时间】:2012-10-14 03:18:55
【问题描述】:

我创建了一个核心数据模型,其中包含三个实体,分别称为 Customer、Form1 和 Form2。我已经建立了从 Customer 到 form1 和 form2 的反向关系,反之亦然。因此,当创建新客户时,需要为该客户创建 form1 和 form2。我遇到的问题是在客户被删除后为每个客户访问正确的 form1 或 form2。那么我如何正确地从所有三个实体中添加和删除对象。让我知道我是否以正确的方式做所有事情,因为我之前从未在 uitableview 中使用过多个实体。任何帮助都会很棒!

-(void) viewDidLoad  
{
// Generate a fetch request for reading from the database and set its entity property
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName: @"Customer" inManagedObjectContext: managedObjectContext];
request.entity = entity;
[entity release];

// Perform the actual fetch from the permanent store and initialize the menuItems array with the results
NSError *error = nil;

customerArray = [[managedObjectContext executeFetchRequest: request error: &error] 

[request release];
}




- (void) addItem
{
// Insert a new record in the database
Customer * customerItem = [NSEntityDescription insertNewObjectForEntityForName: @"Customer" inManagedObjectContext: managedObjectContext];
NSError *error = nil;

// Insert a new item in the table's data source
[customerArray insertObject: customerItem atIndex: 0];

[NSEntityDescription insertNewObjectForEntityForName: @"Form1" inManagedObjectContext: managedObjectContext];

[NSEntityDescription insertNewObjectForEntityForName: @"Form2" inManagedObjectContext: managedObjectContext];
[managedObjectContext save: &error];

// Insert a new row in the table
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
[table insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
{
    // Delete item from the context and save the context. This removes the item from the permanent store.
    [managedObjectContext deleteObject: [customerArray objectAtIndex: indexPath.row]];
    NSError *error = nil;
    [managedObjectContext save: &error];

    // Remove the item from the table's data source array
    [customerArray removeObjectAtIndex: indexPath.row];

    // Delete the item from the table itself.
    [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: YES];
}

}

【问题讨论】:

    标签: iphone xcode uitableview core-data


    【解决方案1】:

    因为您已经建立了从客户到 form1 和 form2 的关系。您可以从客户那里访问 form1 和 form2。

    Raywinderlich 有一个nice tutorial 用于演示核心数据与关系,实现方式相同。

    【讨论】:

    • 所以你说我应该通过这样做来添加和访问form1和form2:textFieldQ1.text = customer.form1.question1。我要试试这个。我会让你知道这一切是如何进行的。
    • 我现在无法保存它。在 viewWillDisappear 我这样做 NSManagedObjectContext *context = customer.form1.managedObjectContext;错误=无; [上下文保存:&error];但它什么也没做。所以我发现 customer.form1.managedObjectContext = null 但 customer.managedObjectContext 不为空。我如何传递以保存 customer.form1
    猜你喜欢
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    相关资源
    最近更新 更多