[英]How to delete existing records and add new datas in core data
以下是我的將記錄添加到核心數據數據庫的代碼。...endingShipmentDAO是一個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"]];
}
}
在使用您的實體名稱在核心數據中添加新數據之前,只需調用以下方法即可。
-(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];
}
在同一托管對象上下文中同步執行操作(刪除,插入)。
您需要根據並發類型之一創建托管對象上下文(MOC): NSPrivateQueueConcurrencyType
或NSMainQueueConcurrencyType
。
使用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 (eg [pendingShipment setValue:...) // save MOC if ([context hasChanges]) { (void)[context save:&error]; } }];
注意:如果需要在塊內引用MOC,請使用context
。
有關詳細信息,請參見《 核心數據編程指南:並發性 》。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.