CoreData在设备上发生错误,但在模拟器上很好

时间:2014-11-06 11:18:10

标签: ios core-data nsmanagedobject nsmanagedobjectcontext magicalrecord

我只是尝试使用核心数据来做某事,我的目标是创建一个带有子上下文的对象,为对象分配值,然后在执行某些操作后将其保存到存储中。

它在模拟器上运行正常,但在设备上运行时发生错误。

这是错误:

  

CoreData:错误:变更托管对象0x174033540      (0x1740cb600)从其上下文中删除后。

我的代码:

Extestion

extension NSManagedObject {
    class func GV_New() -> NSManagedObject {
        let entity = self.MR_entityDescription();
        let parentContext = NSManagedObjectContext.MR_defaultContext();
        let childContext = NSManagedObjectContext.MR_contextWithParent(parentContext);

        let managedObject = NSManagedObject(entity: entity, insertIntoManagedObjectContext: childContext);

        return managedObject;
    }

    class func GV_Insert(object: NSManagedObject) -> Bool {
        var result: Bool = false;

        let keyPath = "userInfo.primaryKey";
        let primaryKey = self.MR_entityDescription().valueForKeyPath(keyPath) as String!;

        if primaryKey != nil {
            if (self.MR_findFirstByAttribute(primaryKey, withValue: object.valueForKey(primaryKey)) != nil) {
                result = false;
            } else {
                let managedObjectId = object.objectID;
                let managedObjectContext = object.managedObjectContext;
                let managedObject = managedObjectContext!.existingObjectWithID(managedObjectId, error: nil);
                managedObject?.managedObjectContext?.MR_saveOnlySelfAndWait();
                NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait();

                result = true;
            }
        }
        return result;
    }
}

的ViewController

class ViewController: UIViewController {
    var camera: Camera!;
    override func viewDidLoad() {
        super.viewDidLoad();

        println("\(Camera.MR_countOfEntities())");

        camera = Camera.GV_New() as Camera;
        camera.camera_id = 4567;
        Camera.GV_Insert(camera);

        println("\(Camera.MR_countOfEntities())");

        for (index, value) in enumerate(Camera.MR_findAll()) {
            let result = value as Camera;
            println("result: \(value.camera_id)");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

刚刚解决了这个问题,

我还没有保留子上下文,因为swift还没有支持类变量,

只需在文件上设置managedContext。

  

让managedContext = NSManagedObjectContext.MR_context();

let managedContext = NSManagedObjectContext.MR_context();

extension NSManagedObject {

    class func GV_New() -> NSManagedObject {

        return self.MR_createInContext(managedContext) as NSManagedObject;

    }

    class func GV_Insert(object: NSManagedObject) -> Bool {

        var result: Bool = false;

        let keyPath = "userInfo.primaryKey";

        let primaryKey = self.MR_entityDescription().valueForKeyPath(keyPath) as String!;

        if primaryKey != nil {

            if (self.MR_findFirstByAttribute(primaryKey, withValue: object.valueForKey(primaryKey)) != nil) {

                result = false;

            }

            else {

                object.managedObjectContext?.MR_saveOnlySelfAndWait();

                NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait();

                result = true;

            }

        }

        return result;

    }

}