2016-11-13 6 views
2

文字列配列の最後の項目のみが必要なコアデータエンティティに保存されるという問題があります。配列データがコアデータエンティティに正しく保存されない

私は "itemNames"と呼ばれる多次元配列を持っています。私はこの配列を単純な1次元配列にマップしてそれを反復処理します。 ループは、すべてのアイテムに対して、属性 "calc_item"のコアデータエンティティ "CALC_VALUES"に保存されるnewItemを作成します。

newItemのコンソール出力は、アスペクトとして表示されます。

エンティティ "CALC_VALUES"のコアデータエントリを読み込むと、コンソール出力2が表示されます。最後のアイテム "item9"のみが、エンティティに保存されています(コンソール出力2参照) 。

私のコードは、配列全体の繰り返し処理ですべてのアイテムを保存するために変更する必要がありますか?

コード(SWIFT 3):

コンソール出力チェックポイント1:

***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6080000ad4a0> (entity: CALC_VALUES; id: 0x608000232a00 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F3> ; data: { 
    "calc_item" = "item1"; 
    "calc_value" = 0; 
}) 
***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6000000ac1e0> (entity: CALC_VALUES; id: 0x600000235320 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F4> ; data: { 
    "calc_item" = "item2"; 
    "calc_value" = 0; 
}) 
***This is newItem: *** <StartUpKit.CALC_VALUES: 0x6000000ab880> (entity: CALC_VALUES; id: 0x600000236c80 <x-coredata:///CALC_VALUES/t02F40B3E-95D3-4692-B630-2AEF16129A5F5> ; data: { 
    "calc_item" = "item3"; 
    "calc_value" = 0; 
}) 

... 

コンソール出力チェックポイント2:

***This is calc_values: *** [<StartUpKit.CALC_VALUES: 0x6000000aac20> (entity: CALC_VALUES; id: 0xd000000001f00000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p124> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aabc0> (entity: CALC_VALUES; id: 0xd000000001f40000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p125> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aab60> (entity: CALC_VALUES; id: 0xd000000001f80000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p126> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aab00> (entity: CALC_VALUES; id: 0xd000000001fc0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p127> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aaaa0> (entity: CALC_VALUES; id: 0xd000000002000000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p128> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aaa40> (entity: CALC_VALUES; id: 0xd000000002040000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p129> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa9e0> (entity: CALC_VALUES; id: 0xd000000002080000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p130> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa980> (entity: CALC_VALUES; id: 0xd0000000020c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p131> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa680> (entity: CALC_VALUES; id: 0xd000000002100000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p132> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000aa5c0> (entity: CALC_VALUES; id: 0xd000000002140000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p133> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9ea0> (entity: CALC_VALUES; id: 0xd000000002180000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p134> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9d80> (entity: CALC_VALUES; id: 0xd0000000021c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p135> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9de0> (entity: CALC_VALUES; id: 0xd000000002200000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p136> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9cc0> (entity: CALC_VALUES; id: 0xd000000002240000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p137> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a99c0> (entity: CALC_VALUES; id: 0xd000000002280000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p138> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9900> (entity: CALC_VALUES; id: 0xd0000000022c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p139> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9240> (entity: CALC_VALUES; id: 0xd000000002300000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p140> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9120> (entity: CALC_VALUES; id: 0xd000000002340000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p141> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9180> (entity: CALC_VALUES; id: 0xd000000002380000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p142> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a9060> (entity: CALC_VALUES; id: 0xd0000000023c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p143> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8d60> (entity: CALC_VALUES; id: 0xd000000002400000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p144> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8ca0> (entity: CALC_VALUES; id: 0xd000000002440000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p145> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a6240> (entity: CALC_VALUES; id: 0xd000000002480000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p146> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a84c0> (entity: CALC_VALUES; id: 0xd0000000024c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p147> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac3c0> (entity: CALC_VALUES; id: 0xd000000002500000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p148> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac420> (entity: CALC_VALUES; id: 0xd000000002540000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p149> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000a8040> (entity: CALC_VALUES; id: 0xd000000002580000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p150> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ac480> (entity: CALC_VALUES; id: 0xd0000000025c0000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p151> ; data: <fault>), <StartUpKit.CALC_VALUES: 0x6000000ab040> (entity: CALC_VALUES; id: 0xd000000002600000 <x-coredata://7687B389-1346-4C27-9B93-99B324FC28D2/CALC_VALUES/p152> ; data: { 
    "calc_item" = "item9"; 
    "calc_value" = 0; 
})] 
+0

ループarfterあなたのコンテキストを保存すると動作を変更する場合は、試してください: 'てみてください! context.save() ' – shallowThought

+0

あなたのエンティティCALC_VALUESクラスの様子を表示できますか? –

答えて

1

(UIApplication.shared.delegate as! AppDelegate).saveContext()はforループの外側に置かなければなりません。

以下は完全なコードです。あなたは以下SQLite Manager

を使用することができ、あなたのデシベルのchanagesを表示するには

let itemNames = [["item1", "item2", "item3", "item4", "item5"], ["item6", "item7", "item8", "item9"]] 
    let flatItemNames = itemNames.flatMap({ $0 }) 

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    for itemName in flatItemNames { 
     let managedContext = NSEntityDescription.insertNewObject(forEntityName: "CALC_VALUES", into: context) 
     managedContext.setValue(itemName, forKey: "calc_item") 
    } 
    do { 
     try context.save() 
    } catch let error as NSError { 
     print("Error While Saving Data: \(error.userInfo)") 
    } 

はスクリーンショットです。デバッグの理由

enter image description here

+0

完璧に動作します!ご協力いただきありがとうございます! – Jake2Finn

2

一見すると、同じキー "calc_item"で各アイテムを保存しているように見えます。したがって、それぞれの保存は前のものを上書きしています。

+0

ええ、それは理にかなっています。 'newItem.setValue(itemName、forKey:" calc_item ")'という文字列を属性 "calc_item"のflatItemNamesから挿入する別の方法がありますか? – Jake2Finn

+0

'(UIApplication.shared.delegate as!AppDelegate).saveContext()'をforループの外側に保ちます。私はこの回答を更新しましたが、ピアレビューは完了しています。 –

関連する問題