It doesn't work well when all objects you want to add are new and haven't been inserted before. Try to first insert the Item object before adding the SubItem objects.
For example
let item = Item(name: "Item1", subitems: [])
container.mainContext.insert(item)
item.subitems.append(SubItem(name: "subItemA"))
item.subitems.append(SubItem(name: "subItemB"))
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: