从核心数据中获取值始终返回null

时间:2016-03-21 13:07:20

标签: ios objective-c

我正在解析一个json并将这些值作为记录添加到核心数据中。我必须保存这些数据并使用核心数据将它们添加到db。我使用以下代码来做到这一点,

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。为什么会这样?

1 个答案:

答案 0 :(得分:1)

即使self.pendingShipmentDAOnil,您仍尝试在else语句中为其分配值,而不是使用pendingShipment,并将其插入到托管对象上下文中。执行提取时,您会收到空的pendingShipment对象。

因此,您需要以下列方式更改代码:

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];
    [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:pointOfContact forKey:@"point_of_contact"];
    [pendingShipment setValue:shipmentNumber forKey:@"empid"];
    [pendingShipment setValue:shipmentNumber forKey:@"products"];
    [pendingShipment setValue:shipmentNumber forKey:@"quantity"];
    [pendingShipment setValue:shipmentNumber forKey:@"rate"];
    [pendingShipment setValue:shipmentNumber forKey:@"amount"];
    [pendingShipment setValue:shipmentNumber forKey:@"product_image"];
    [pendingShipment setValue:pendingStatus forKey:@"status"];
}

另外,我不确定您为什么要在此if语句之前在托管对象上下文中插入新实体。看起来像一个错误:

NSManagedObject *pendingShipment = [NSEntityDescription insertNewObjectForEntityForName:@"PendingShipmentDetails" inManagedObjectContext:context];
// You never use this pendingShipment object!

if (self.pendingShipmentDAO) {
    // ...