jsonを解析し、その値をレコードとしてコアデータに追加します。私はこれらのデータを保存し、コアデータを使ってdbに追加する必要があります。イムそうするために、次のコードを使用して、コアデータから値をフェッチすると常にNULLが返されます
for (NSDictionary *response in details) {
NSString *invoiceNo = [response valueForKey:@"invoice_no"];
NSString *shipmentNumber = [response valueForKey:@"shipment_no"];
NSString *invoiceDate = [response valueForKey:@"invoice_date"];
NSString *proformaInvoiceNumber = [response valueForKey:@"proforma_invoice_no"];
NSString *proformaInvoiceDate = [response valueForKey:@"proforma_invoice_date"];
NSString *plannedShipmentDates = [response valueForKey:@"planned_shipment_dates"];
NSString *pointOfContact =[[response objectForKey:@"point_of_contact"] valueForKey:@"empid"];
NSString *pendingStatus = [response valueForKey:@"status"];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];
if (self.pendingShipmentDAO) {
// Update existing device
[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:pointOfContact forKey:@"point_of_contact"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"empid"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"products"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"quantity"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"rate"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"amount"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"product_image"];
[self.pendingShipmentDAO setValue:pendingStatus forKey:@"status"];
} else {
// Create a new device
NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];
[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:pointOfContact forKey:@"point_of_contact"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"empid"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"products"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"quantity"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"rate"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"amount"];
[self.pendingShipmentDAO setValue:shipmentNumber forKey:@"product_image"];
[self.pendingShipmentDAO setValue:pendingStatus forKey:@"status"];
}
//
}
NSError *error = nil;
// Save the object to persistent store
NSManagedObjectContext *context = [self managedObjectContext];
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PendingShipmentDetails" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (result.count > 0) {
NSManagedObject *pending = (NSManagedObject *)[result objectAtIndex:0];
// NSLog(@"1 - %@", pending);
NSLog(@"statsus %@ %@", [pending valueForKey:@"status"], [pending valueForKey:@"shipmentno"]);
// NSLog(@"2 - %@", pending);
}
問題はNSLog(@"statsus %@ %@", [pending valueForKey:@"status"], [pending valueForKey:@"shipmentno"]);
は常にnullを返しますです。どうしてこんなことに?
「pendingShipmentDAO」とは何ですか? – vadian
そのNSObjectModel –
しかし、 'pendingShipment'と' pendingShipmentDAO'の関係は何ですか?新しく作成された 'pendingShipment'は決してそのコードで使われません。 – vadian