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

ios – CoreData在设备上发生错误但在模拟器上很好,第1张

概述我只是尝试使用核心数据来做某事,我的目标是创建一个带有子上下文的对象,为对象分配值,然后在执行某些 *** 作后将其保存到存储中. 它在模拟器上工作正常,但在设备上运行时发生错误. 这是错误: CoreData: error: Mutating a managed object 0x174033540 (0x1740cb600) after it has been removed from its con 我只是尝试使用核心数据来做某事,我的目标是创建一个带有子上下文的对象,为对象分配值,然后在执行某些 *** 作后将其保存到存储中.

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

这是错误:

CoreData: error: Mutating a managed object 0x174033540

(0x1740cb600) after it has been removed from its context.

我的代码:

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;    }}

视图控制器

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)");        }    }}
解决方法 刚解决了这个问题,

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

只需在文件上设置managedContext.

let 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;    }}
总结

以上是内存溢出为你收集整理的ios – CoreData在设备上发生错误但在模拟器上很好全部内容,希望文章能够帮你解决ios – CoreData在设备上发生错误但在模拟器上很好所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存