简体   繁体   English

CoreData:将对象从一个ArrayController移到另一个

[英]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. 我有两个使用Core Data实体的NSArrayControllers ,我想将某些索引处的对象(通过NSIndexSet )从一个AC移动到另一个AC。 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. 通常,这很容易,但是这并不是因为核心数据位于其中,如果我尝试使用它,它似乎是从源AC中删除对象,而不是将它们添加到目标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? 我需要做什么才能让Core Data考虑到删除和添加?

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. 我通过创建目标(NSManagedObject)实体的新对象并将所有数据传输到该对象来解决了该问题。 It was indeed an issue because the core data entities of the two ACs is different. 确实存在问题,因为两个AC的核心数据实体不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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