0

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"

4

1 回答 1

1

简短的回答是你需要移动这条线:

let new_Ent2_Item = NSEntityDescription.insertNewObjectForEntityForName("Entity2", inManagedObjectContext: managedContext)

使其位于for 循环内:

for i in 0..<entity1.count {
    //Grab a Today task item
    let itemToMove = entity1_Cntxt[i]
    // Create the corresponding new Entity2 object:
    let new_Ent2_Item = NSEntityDescription.insertNewObjectForEntityForName("Entity2", inManagedObjectContext: managedContext)
    //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)
}

解释一下:insertNewObjectForEntityForName这实际上是创建新的 Entity2 对象。在其原始位置,该行仅运行一次,因此仅创建一个 Entity2 对象。然后,您的for循环会更改其属性值并将其插入到数组的开头entity2_Cntxt(多次)。请注意,for循环中的最后一步entity2_Cntxt.insert(new_Ent2_Item, atIndex: 0)不会创建新对象或复制new_Ent2_Item,它只是将其插入到数组的开头。使用修改后的代码,每次循环都会创建一个新的 Entity2 对象。

关于这个displayOrder问题,如果您entity2_Cntxt每次通过循环检查数组,您应该发现(使用您的原始代码)该数组多次包含相同的 Entity2 对象,然后是从 fetch 中获得的对象。假设entity2_Cntxt包含新对象三次(在索引 0、1、2 处)。然后,您的update_TargetDisplayOrder方法将该对象设置displayOrder为 0,然后是 1,然后是 2。然后 fetch 返回的对象将具有displayOrder3、4 等。我认为当您insertNewObjectForEntityForName按上述方式移动时,这一切都会好起来的。

于 2015-09-24T23:22:35.203 回答