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

时间:2016-03-23 08:41:19

标签: ios objective-c iphone database core-data

以下是我的代码将记录添加到核心数据db .... 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"]];

        }

    }

2 个答案:

答案 0 :(得分:1)

在使用您的实体名称在核心数据中添加新数据之前,请调用以下方法。

-(void)deleteAllPreviousData {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Your Entity Name" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSManagedObjectContext *context = [self managedObjectContext];
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"Could not delete Entity Objects");
    }
    for (NSManagedObject *currentObj in fetchedObjects) {
        [self.managedObjectContext deleteObject:currentObj];
    }
    [context save:&error];
}

答案 1 :(得分:1)

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

  1. 您需要使用其中一种并发类型创建托管对象上下文(MOC):ConstraintDefinitionException('"%max_filesize%" is not a valid maximum size')NSPrivateQueueConcurrencyType,具体取决于您的需求。

  2. 使用NSMainQueueConcurrencyType做事

    performBlockAndWait:
  3. 注意:如果您需要引用块中的MOC ,请使用[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]; } }];

    有关详细信息,请参阅Core Data Programming Guide: Concurrency