gpt4 book ai didi

ios - 如何删除现有记录并在核心数据中添加新数据

转载 作者:行者123 更新时间:2023-11-28 21:30:47 25 4
gpt4 key购买 nike

以下是我将记录添加到核心数据数据库的代码....pendingShipmentDAO 是一个 NSObjectModel。问题在于,每次单击按钮时都会嵌入新数据。如果我的数据库中已经存在数据并插入新数据,我如何清除数据库中数据中的所有记录?

if((self.pendingShipmentDAO) )
{
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"shipmentno"];
[self.pendingShipmentDAO setValue:proformaInvoiceNumber forKey:@"proforma_invoice_no"];
[self.pendingShipmentDAO setValue:proformaInvoiceDate forKey:@"proforma_invoice_date"];
[self.pendingShipmentDAO setValue:invoiceNo forKey:@"invoice_no"];
[self.pendingShipmentDAO setValue:invoiceDate forKey:@"invoice_date"];
[self.pendingShipmentDAO setValue:plannedShipmentDates forKey:@"planned_shipment_date"];
[self.pendingShipmentDAO setValue:address forKey:@"point_of_contact"];
[self.pendingShipmentDAO setValue:address forKey:@"empid"];
[self.pendingShipmentDAO setValue:name forKey:@"products"];
[self.pendingShipmentDAO setValue:qty forKey:@"quantity"];
[self.pendingShipmentDAO setValue:rte forKey:@"rate"];
[self.pendingShipmentDAO setValue:amt forKey:@"amount"];
[self.pendingShipmentDAO setValue:img forKey:@"product_image"];
[self.pendingShipmentDAO setValue:pendingStatus forKey:@"status"];
}
else
{
NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];
[pendingShipment setValue:shipmentNumber forKey:@"shipmentno"];
[pendingShipment setValue:proformaInvoiceNumber forKey:@"proforma_invoice_no"];
[pendingShipment setValue:proformaInvoiceDate forKey:@"proforma_invoice_date"];
[pendingShipment setValue:invoiceNo forKey:@"invoice_no"];
[pendingShipment setValue:invoiceDate forKey:@"invoice_date"];
[pendingShipment setValue:plannedShipmentDates forKey:@"planned_shipment_date"];
[pendingShipment setValue:address forKey:@"point_of_contact"];
[pendingShipment setValue:address forKey:@"empid"];
[pendingShipment setValue:name forKey:@"products"];
[pendingShipment setValue:qty forKey:@"quantity"];
[pendingShipment setValue:rte forKey:@"rate"];
[pendingShipment setValue:amt forKey:@"amount"];
[pendingShipment setValue:img forKey:@"product_image"];
[pendingShipment setValue:pendingStatus forKey:@"status"];
}

更新:

这就是我获取数据的方式,

NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"PendingShipmentDetails"];


NSEntityDescription *entity = [NSEntityDescription entityForName:@"PendingShipmentDetails" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:&error];
if (result.count > 0) {
int i;
amountArray = [[NSMutableArray alloc] init];
statusArray = [[NSMutableArray alloc]init];
shipmentReferenceNumberArray = [[NSMutableArray alloc]init];
invoiceNumberArray = [[NSMutableArray alloc]init];
for (i = 0; i < [result count]; i++) {
//NSLog(@"%@", result);
pending = (NSManagedObject *)[result objectAtIndex:i];
NSLog(@"pro inv no %@",[pending valueForKey:@"proforma_invoice_no"]);
NSLog(@"shipment no %@",[pending valueForKey:@"shipmentno"]);
NSLog(@"pro inv date %@",[pending valueForKey:@"proforma_invoice_date"]);
NSLog(@"inv no %@",[pending valueForKey:@"invoice_no"]);
NSLog(@"inv date %@",[pending valueForKey:@"invoice_date"]);
NSLog(@"pl sh date %@",[pending valueForKey:@"planned_shipment_date"]);
NSLog(@"pt ct %@",[pending valueForKey:@"point_of_contact"]);
NSLog(@"pro %@",[pending valueForKey:@"products"]);
NSLog(@"qty %@",[pending valueForKey:@"quantity"]);
NSLog(@"rte %@",[pending valueForKey:@"rate"]);
NSLog(@"amt %@",[pending valueForKey:@"amount"]);
NSLog(@"pro imng %@", [pending valueForKey:@"product_image"]);
NSLog(@"statsus %@", [pending valueForKey:@"status"]);

// amountArray = [[NSMutableArray alloc]initWithObjects:[pending valueForKey:@"amount"], nil];
[amountArray addObject:[pending valueForKey:@"amount"]];
[statusArray addObject: [pending valueForKey:@"status"]];
[shipmentReferenceNumberArray addObject:[pending valueForKey:@"shipmentno"]];
[invoiceNumberArray addObject:[pending valueForKey:@"invoice_no"]];

}

}

最佳答案

在同一托管对象上下文中同步执行操作(删除、插入)。

  1. 您需要使用以下并发类型之一创建托管对象上下文 (MOC):NSPrivateQueueConcurrencyTypeNSMainQueueConcurrencyType,具体取决于您的需要。

  2. 使用performBlockAndWait:做事

    [context performBlockAndWait:^{

    // remove all rows
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"PendingShipmentDetails"];
    NSError *error;
    NSArray * pendingShipments = [context executeFetchRequest:request error:&error];
    for (NSManagedObject *pendingShipment in pendingShipments) {
    [context deleteObject: pendingShipment];
    }

    // add new row(s)

    NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];

    // TODO: set values here (e.g. [pendingShipment setValue:...)

    // save MOC
    if ([context hasChanges]) {
    (void)[context save:&error];
    }
    }];

注意:如果您需要在 block 内引用MOC,请使用context

参见 Core Data Programming Guide: Concurrency了解详情。

关于ios - 如何删除现有记录并在核心数据中添加新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173338/

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