I have two entities and they have the exact same three attributes (name, desc, displayOrder), both have a handful of records, and my goal is to add/insert every item from "Entity 1" into "Entity 2".
Close, But No Cigar
I think my code is quite close. The console print out shows my code is successfully sending each item in "Entity 1" to "Entity 2" and saving it. BUT they save over each other! The first item is moved and saved, then the second item is moved but copies over the item that was previously moved. End result: only the last item moved actually shows up in the final "Entity 2".
Question: How do I fix this?
@IBAction func testOutMoveList(sender: AnyObject) {
//Setup 'Do Later' context
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let new_Ent2_Item = NSEntityDescription.insertNewObjectForEntityForName("Entity2", inManagedObjectContext: managedContext)
//Get sorted CoreData list and assign it to targetList_Cntxt
let fetchRequest = NSFetchRequest(entityName: "Entity2")
let sortDescriptor = NSSortDescriptor(key: "displayOrder", ascending: true)
fetchRequest.sortDescriptors = [ sortDescriptor ]
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
if let results = fetchedResults {
entity2_Cntxt = results
}
} catch {
print(error)
}
for i in 0..<entity1.count {
//Grab a Today task item
let itemToMove = entity1_Cntxt[i]
//Assign the Today item's contents to variables
let nameToTransfer = itemToMove.valueForKey("name") as? String
let descToTransfer = itemToMove.valueForKey("desc") as? String
//Assign the Today item's contents to the target's 'task' object
new_Ent2_Item.setValue(nameToTransfer, forKey: "name")
new_Ent2_Item.setValue(descToTransfer, forKey: "desc")
//Insert the item!!
entity2_Cntxt.insert(new_Ent2_Item, atIndex: 0)
}
//Updates target list in Core Data after append, delete, and drag/drop
func update_TargetDisplayOrder() {
for i in 0..<entity2_Cntxt.count {
let item = entity2_Cntxt[i]
item.setValue( i, forKey: "displayOrder" )
}
}
Possible Clue: I have noticed that the displayOrder doesn't seem to be updating correctly. The first item should be 0, the second should be 1, etc but instead each time the code cycles the lowest displayOrder is 1 higher (A three item list might start with values 2,3,4 - then my code moves/copies another item to entity 2 and the display order values are: 3,4,5)
Question: What code can I add or fix to make this transfer I'm attempting work!?!
Bonus question: Post-transfer how do I quickly/easily clear all the values from "Entity 1"