2016-03-21 6 views
0

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を返しますです。どうしてこんなことに?

+0

「pendingShipmentDAO」とは何ですか? – vadian

+0

そのNSObjectModel –

+0

しかし、 'pendingShipment'と' pendingShipmentDAO'の関係は何ですか?新しく作成された 'pendingShipment'は決してそのコードで使われません。 – vadian

答えて

1

self.pendingShipmentDAOnil場合でも、あなたはまだあなたが管理対象オブジェクトコンテキストに挿入されたことを、代わりにpendingShipmentを使用しての、あなたのelse声明の中で、それに値を代入してみてください。フェッチを実行すると、空の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) { 
    // ... 
+0

ありがとうございますが、挿入された最後の値だけが表示されます。とにかくアーリーリストの形ですべてのレコードをdbで取得できるのですか? –

+1

'result'はNSArrayですが、あなたは' [result objectAtIndex:0] 'を介して単一のオブジェクトしか使用していません –

+0

ありがとうございます... –

関連する問題