1

我有以下文件:

ProductViewController.h
ProductViewController.m
ComponentsViewController.h
ComponentsViewController.m

在里面我有我的按钮,我使用核心数据ProductViewController.m将产品信息保存到我的sqlite数据库中。在里面ComponentsViewController.m我有我的按钮来保存组件的信息到我的sqlite。我在我的数据模型中创建了一对多(产品到组件)关系。我的问题是当我保存我的组件信息而不是将组件分配给我已经创建的产品时,信息会保存到当我按下保存组件按钮时创建的“空”产品。在我的代码下方:

ProductViewController.m

- (IBAction)saveProductButton:(id)sender {




    // Add product info to Data base

Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:self.managedObjectContext];

    newProduct.prdname = self.name.text;
newProduct.prdtdesc = self.prdtdesc.text;
newProduct.prdtid = self.prdtid.text;

    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
    //  4
    self.name.text = @"";
    self.prdtid.text = @"";
    self.prdtdesc.text = @"";

    //  5
    [self.view endEditing:YES];
}


ComponentsViewController.m

- (IBAction)addComponent:(id)sender {



Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:self.managedObjectContext];

 Components * newComponent = [NSEntityDescription insertNewObjectForEntityForName:@"Components"
 inManagedObjectContext:self.managedObjectContext];
 newComponent.prdtcompname = self.prdtcompname.text;
newComponent.prdtcompid = self.prdtcompid.text;
newComponent.prdtcompdesc = self.prdtcompdesc.text;
newComponent.prdtcompqt = self.prdtcompQT.text;

newProduct.tocomponents = [NSSet setWithObjects:newComponent, nil];


 NSError *error;
 if (![self.managedObjectContext save:&error]) {
 NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
 }
 //  4

 self.prdtcompQT.text = @"";
 self.prdtcompname.text = @"";
 self.prdtcompid.text = @"";
 self.prdtcompdesc.text = @"";
 //  5
 [self.view endEditing:YES];

}
4

2 回答 2

1

不要在创建组件时创建新产品。创建组件时,只需获取具有相关 ID 的产品并将产品引用存储在组件中。

这是我的示例代码:

我创建了一种相同的一对多关系(Item -> Item_Details)

第 1 步:首先我在项目实体中进行插入。

第 2 步:在插入 item_Details 时,我只是使用相对 ID 从 Item 实体中获取 item.info。

一旦你创建了一个 NSManagedObjectSubClass (有关系),它会自动创建你提到的引用实例。

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Item;

@interface Item_Detail : NSManagedObject


@property (nonatomic, retain) NSNumber * item_Id;      // Item_Id is a common id in both item and Item_details should act as a relation.
@property (nonatomic, retain) NSString * item_Size;
@property (nonatomic, retain) NSString * last_Modified;
@property (nonatomic, retain) NSNumber * price;

@property (nonatomic, retain) Item *itemInfo;  // RelationShip reference

@end

// 使用关系 ID 获取项目信息:

 +(Item *)getSourceItemDetail:(NSNumber *)itemId
    {
        NSEntityDescription *itemEntity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[ASDataManager managedObjectContext]];
        NSFetchRequest *itemFetchRqt= [[NSFetchRequest alloc]init];
        [itemFetchRqt setEntity:itemEntity];

        NSPredicate *itempredicate = [NSPredicate predicateWithFormat:@"item_Id = %@",itemId];
        [itemFetchRqt setPredicate:itempredicate];
        NSError *error = nil;
        NSArray *resultArray = [[ASDataManager managedObjectContext] executeFetchRequest:itemFetchRqt error:&error];

        if([resultArray count] == 1)
        {
            Item *item = (Item *)[resultArray objectAtIndex:0];
            return item;
        }
        return nil;
    }


//Item_Detail _Insertion
+(void)insertNewItemDetailRecordInItemDetailTable:(NSDictionary *)itemDetailDic
{

    Item_Detail *item_Dtls = (Item_Detail *)[NSEntityDescription insertNewObjectForEntityForName:@"Item_Detail" inManagedObjectContext:[ASDataManager managedObjectContext]];


    item_Dtls.item_Id = [NSNumber numberWithInt:[[itemDetailDic valueForKey:@"Item_ID"] intValue]];
    item_Dtls.item_Size = [ASGlobalInfoHelper getValueIfNotNullFrom:[itemDetailDic objectForKey:@"Item_Size"] valueType:ValueTypeString];

    item_Dtls.itemInfo = [self getSourceItemDetail:[NSNumber numberWithInt:@"90"]];
    //Getting Relationship data
    if(![[ASDataManager managedObjectContext] save:nil])
        NSLog(@"Error in Save");


}
于 2013-10-01T04:14:38.330 回答
0

ComponentsViewController.m中,您在此行中创建了一个空产品:

Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:self.managedObjectContext];

如果您想将新创建Components的对象与先前创建的Product对象建立关系,您要么必须使用 FetchRequest 从 Core Data 获取 Product ,要么将 fromProduct传递ProductViewControllerComponentsViewController

于 2013-10-01T03:36:42.197 回答