2016-03-21 27 views
0

Bir json ayrıştırmak ve bu değerleri çekirdek verilere kayıt olarak eklemek. Bu verileri kaydetmem ve ana verileri kullanarak db'ye eklemem gerekiyor. imçekirdek verilerinden veri getirme her zaman null değerini döndürür

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); 
} 

sorun NSLog(@"statsus %@ %@", [pending valueForKey:@"status"], [pending valueForKey:@"shipmentno"]); her zaman null döndürür, bu yüzden yapmak için aşağıdaki kodu kullanarak. Neden böyle?

+0

'pendingShipmentDAO' nedir? – vadian

+0

bir NSObjectModel –

+0

Ancak pendingShipment ve pendingShipmentDAO arasındaki ilişki nedir? Yeni oluşturulan "pendingShipment" bu kodda hiç kullanılmamaktadır. – vadian

cevap

1

self.pendingShipmentDAOnil olsa bile, yine de yönetilen nesne bağlamı içine yerleştirilmiş olduğu, yerine pendingShipment kullanmak yerine, sizin else açıklamada kendisine değerleri atamak deneyin. Getirdiğinizde boş pendingShipment nesnesini alırsınız.

Yani, şu şekilde kodunu değiştirmek gerekir: Bu if deyimi önce yönetilen nesne bağlamında yeni bir varlık ekliyorsanız neden

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"]; 
} 

Ayrıca, ben emin değilim. Hata gibi görünüyor:

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

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

teşekkür ederim çalışır, ancak sadece eklenen son değeri yazdırır. Db'de bütün sicili arraylist şeklinde alabileceğim bir yer var mı? –

+1

'result' bir“ NSArray ”dır, ancak [result objectAtIndex: 0]' –

+0

aracılığıyla yalnızca tek bir nesneyi kullanıyorsunuz ... bir çekicilik gibi çalışıyor –

İlgili konular