简体   繁体   中英

CoreData: Move objects from one ArrayController to another

I have two NSArrayControllers that use Core Data entities and I want to move objects at certain indices (via NSIndexSet ) from one AC to the other. Normally this would be easy but here it's not because Core Data is under it and if I try it, it seems to remove the objects from the source AC but not adding them to the target AC. I'm using this code:

extension NSIndexSet
{
    func toArray() -> [Int]
    {
        var indexes:[Int] = [];
        self.enumerateIndexesUsingBlock
            {
                (index:Int, _) in
                indexes.append(index);
        }
        return indexes;
    }
}

func moveIndicesToTargetAC(indexSet:NSIndexSet)
{
    let a = indexSet.toArray();
    for i in a
    {
        var obj:NSManagedObject = sourceArrayController?.arrangedObjects.objectAtIndex(i) as! NSManagedObject;
        targetArrayController?.addObject(obj);
        sourceArrayController?.removeObjectAtArrangedObjectIndex(i);
    }
}

What do I need to do to have Core Data regard the deletions and additions?

Thanks for hints anyone! I solved the issue by creating a new object of the target (NSManagedObject) entity and transfer all the data over to it. It was indeed an issue because the core data entities of the two ACs is different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM