Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 核心数据添加新的NSManagedObject_Objective C_Core Data - Fatal编程技术网

Objective c 核心数据添加新的NSManagedObject

Objective c 核心数据添加新的NSManagedObject,objective-c,core-data,Objective C,Core Data,我的核心数据设置如下: 销售代表客户采购协议产品 在应用程序中,销售代表可以更改产品实体的属性,这将触发对购买协议的更改。当他们编辑完工作PA后,他们可以通过web服务提交到我们的CRM(SAP)或将他们的工作保存在本地 我了解(至少我认为我了解:D)如何创建新的NSManagedObject并为其属性添加值: NSManagedObject* newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"PA

我的核心数据设置如下:

销售代表
客户
采购协议
产品

在应用程序中,销售代表可以更改
产品
实体的属性,这将触发对
购买协议的更改。当他们编辑完工作
PA
后,他们可以通过web服务提交到我们的CRM(SAP)或将他们的工作保存在本地

我了解(至少我认为我了解:D)如何创建新的
NSManagedObject
并为其属性添加值:

NSManagedObject* newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"PA" inManagedObjectContext:self.moc];

//get the entity descriptions for PA, Customer, PA_Products and Sales_Rep
NSEntityDescription* PAEntity = [NSEntityDescription entityForName:@"PA" inManagedObjectContext:self.moc];
NSDictionary* dictPAAttributes = [PAEntity attributesByName];
NSArray* arrPAAttributeNames = [dictPAAttributes allKeys];

for(NSString* strThisAttribute in arrPAAttributeNames) {
    [newManagedObject setValue:[self.workingPA valueForKey:strThisAttribute] forKey:strThisAttribute];
}

我将如何添加关系?我是否必须获取新创建的
PA
实体,然后提取产品,从工作PA提取产品的
NSSet
,然后将其添加到新PA?对于
客户
销售代表
实体,流程是否类似?

我想第一个问题是,为什么要有一个工作PA,为什么不让他们直接编辑新的ManagedObject。然后您只需调用
[moc save]

但要创建关系,您需要创建新的关系,因为旧的关系位于workingPA对象和其他对象之间

帮自己一个忙,从喜欢开始

然后假设您拥有它们,您可能还需要创建另一个名为PAItem的对象,以跟踪与PA关联的项目的详细信息(数量、成本等)

因此,假设您有此功能,然后向PA添加项目,您将执行以下操作:

  PurchaseAgreement * newPA = [NSEntityDescription
                               insertNewObjectForEntityForName:@"PurchaseAgreement"
                               inManagedObjectContext:_managedObjectContext];

  newPA.customer = workingPA.customer;
  newPA.attribute1 = workingPA.attribute1;

  for (PAItem *item in workingPA.items) {

     PAItem * newItem = [NSEntityDescription
                               insertNewObjectForEntityForName:@"PAItem"
                               inManagedObjectContext:_managedObjectContext];
     newItem.purchaseAgreement = savingPA;
     newItem.product = item.product;
     newItem.quantity = item.quantity;
     newItem.cost = item.cost;
     . . .

  }
  NSError *error = nil;
  if (![_managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    // Take some action!
  }

谢谢邓肯,昨天我自己也得到了你的答案,只是需要更清楚地思考一下。我们有一个工作PA的原因是,如果PA的状态为final,我们不希望用户更改该数据,但他们可能希望将其作为新PA的起点,因此我们创建一个新PA实体并传输数据并更改其状态,类似这样的情况:if(PA status==4)然后workingPA=copy else workingPA=PA…做点什么
  PurchaseAgreement * newPA = [NSEntityDescription
                               insertNewObjectForEntityForName:@"PurchaseAgreement"
                               inManagedObjectContext:_managedObjectContext];

  newPA.customer = workingPA.customer;
  newPA.attribute1 = workingPA.attribute1;

  for (PAItem *item in workingPA.items) {

     PAItem * newItem = [NSEntityDescription
                               insertNewObjectForEntityForName:@"PAItem"
                               inManagedObjectContext:_managedObjectContext];
     newItem.purchaseAgreement = savingPA;
     newItem.product = item.product;
     newItem.quantity = item.quantity;
     newItem.cost = item.cost;
     . . .

  }
  NSError *error = nil;
  if (![_managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    // Take some action!
  }