gpt4 book ai didi

iphone - 核心数据关系

转载 作者:行者123 更新时间:2023-11-29 13:03:53 26 4
gpt4 key购买 nike

我有以下文件:

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

}

最佳答案

不要在创建组件的同时创建新产品。创建组件时,只需获取具有相关 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");


}

关于iphone - 核心数据关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19107441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com